Learn
Control Flow
Conditional

If the user’s profile is private, hide their posts. Otherwise, make them visible.

Computers can run instructions on their own, leaving us to work on more interesting and creative problems. But computers can’t make decisions on their own. Programmers can give them multiple sets of instructions and describe the right conditions in which to use each set. They can describe a structure like the quoted program above: “if X, then do instruction set 1. Otherwise do instruction set 2.” This structure is called a conditional control structure because the computer’s instructions depend on some condition(s).

In our social media example, all the instructions are shown in the diagram, but only some of them will be executed each time. Here they are in text form:

  1. Check profile privacy
  2. Hide the post
  3. Show the post

When is step 2 executed and when is 3 executed? The answer is: it depends. The control flow is 1-2 if the profile is private and 1-3 if the profile is public. The condition is the privacy setting on the profile.

Conditional control structures, or just conditionals, allow programs to do different things in different scenarios. As you can see, they follow a logic similar to how humans think, making it easy to write clear code while still handling complex processes.

Instructions

Select “Make Profile Public”, then click through each step of the conditional structure.

Select “Make Profile Private”, then click through each step of the conditional structure.

What’s the control flow for each?

Folder Icon

Take this course for free

Already have an account?