Decisions (if statements)

Making a decision based on a single value (if statements)

When the computer needs to “decide” which branch of a flowchart (or algorithm to follow) it evaluates a variable against some condition that we place on it. These decisions are frequently documented in a condition/action table. For instance for the decision below: if age >= 18 we could document it like this:

path condition Action
User is 18 or older age >= 18 Print “Go vote!”
User is less than 18 age < 18 Print “Sorry, not yet”

The diamond shape is used to designate a decision. The “if” is not listed, just the condition.

  • Declare a variable for age
  • Output the instruction to the user and store the result in age
  • Add an IF shape, then double-click the shape to add the condition

properties of if statement

  • Back on the flowchart, click on each branch of the if statement and add an action that the program will take (i.e. the message that is output to the user)

if statement example

Making a decision based on multiple values (if statements with Boolean operators)

In the case of multiple conditions that need to be met, we would use Boolean operators to chain two or more conditions together. Boolean operators:

  • AND – all conditions must be true in order to execute the code
  • OR – only one of the conditions must be true in order to execute the code
  • ! - NOT means that the code will only execute if the condition is not true

For instance for the decision below: if age >= 18 AND enrolled = true we could document it like this:

path condition Action
User is 18 or older age > = 18 AND enrolled = true Print “Go vote!”
User is less than 18 or not enrolled age < 18 OR enrolled = false Print “Sorry, not yet”

example of bool if

Making decisions using multiple values with != (if statements checking for a negative condition)

Many times we need to force the user to enter a certain value that we are looking for. This is especially true when we need to validate the users input before continuing on with the program.

The != operator (not equal) is very useful for this task. The flowchart below asks the user to enter a specific character from the menu. If the condition is met (the user didn’t enter an A, B or C) the true path is chosen. If the condition is not met (user entered an A, B or C) the false path is taken.

Note: We use AND rather than OR here because all conditions need to evaluate to true. For example, they didn’t enter any of the valid inputs (A, B and C).

Multiple values

Nested If (a condition within a condition)

At times we may need to test for a certain condition, and then run a further test on another condition. However, if we want the program to be able to keep track of these individual evaluations we will need a nested conditional statement.

Think of this as an if statement inside another if statement.

In a previous section we were evaluating if a person was old enough to vote AND was enrolled to vote. The condition action table looked like the following:

path condition Action
User is 18 or older age > = 18 AND enrolled = true Print “Go vote!”
User is less than 18 or not enrolled age < 18 OR enrolled = false Print “Sorry, not yet”

In this case we can only respond to the two conditions:

  • Either the user is old enough to vote and is enrolled
  • Resulting in message: “Go vote”
  • Or one of the conditions is false
  • Resulting in the message: “Sorry, not yet”

If we want to be able target each individual condition, we will need a nested structure.

Path Condition Action
User is 18 or older and enrolled age >= 18 AND enrolled = true Print “Go vote!”
User is 18 or older and is not enrolled age >= 18 AND enrolled = false Print “You are old enough to vote, but are not enrolled.”
User is not 18 or older and is enrolled age < 18 AND enrolled = true Print “You are not yet 18 and are enrolled”
User is not 18 or older and is not enrolled age < 18 18 AND enrolled = false Print “You are not yet 18 and are not enrolled”

Nested if exampple

results matching ""

    No results matching ""