← Back to course
2/6
Week 03 · Making Decisions

Tuesday

IF, ELSE, AND, OR
// Booleans, conditions and alert rules
⏱ about 20 min

Tuesday: IF, ELSE, AND, OR

Comet writes Beacon's first alert rule on the whiteboard and underlines it twice.

"If the water is under 30, show The tomatoes need water. Done."

Lena reads it over his shoulder. "And when the water is fine? The screen just stays blank?"

"Blank feels like broken," Wren says. "What do you notice? We only told Beacon what to do half the time."

Nova draws a fork in the road on the whiteboard. "Interesting," she says. "Every IF can have a second path."

Comet grabs the marker. "Then let's build both paths."

Selection and if-statements

Selection decides which parts of an algorithm run, based on a condition being true or false.

Conditional statements, or if-statements, change the usual top-to-bottom flow of a program.

They run different statements based on the value of a Boolean expression.

In our pseudocode, the statements inside the curly braces run only when the condition is true.

waterLevel ← INPUT()
IF (waterLevel < 30)
{
   DISPLAY("The tomatoes need water")
}
DISPLAY("Check complete")
TRACE THE IF
  • Read the question.
  • Tap your answer.
The sensor sends 22. What does Beacon show?
The sensor sends 45. What does Beacon show?

Two paths with ELSE

An IF with an ELSE always takes exactly one of two paths.

If the condition is true, the first block runs. If it is false, the ELSE block runs instead.

IF (waterLevel < 30)
{
   DISPLAY("The tomatoes need water")
}
ELSE
{
   DISPLAY("Water is fine")
}
TRACE THE IF / ELSE
  • Read the question.
  • Tap your answer.
waterLevel is 30. What does Beacon show?
waterLevel is 8. What does Beacon show?

AND and OR

The AP reference sheet provides three logical operators: NOT, AND and OR. Each one evaluates to a Boolean value. This week uses AND and OR.

condition1 AND condition2 is true if both conditions are true. Otherwise it is false.

condition1 OR condition2 is true if either condition is true, or if both are. Otherwise it is false.

waterLevel < 30lightLevel > 80AND givesOR gives
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse
EVALUATE AND AND OR
  • Read the question.
  • Tap your answer.
waterLevel is 20 and lightLevel is 90. Is "waterLevel < 30 AND lightLevel > 80" true or false?
waterLevel is 20 and lightLevel is 50. Is "waterLevel < 30 AND lightLevel > 80" true or false?
waterLevel is 60 and lightLevel is 90. Is "waterLevel < 30 OR lightLevel > 80" true or false?
waterLevel is 60 and lightLevel is 40. Is "waterLevel < 30 OR lightLevel > 80" true or false?
StatementTrue or false?
Selection decides which parts of an algorithm run.?
An IF with an ELSE can run both paths at once.?
AND is true only when both conditions are true.?
OR is false when both conditions are true.?
WHY THIS EXERCISEKnowing exactly when each operator is true keeps alerts from firing at the wrong time.
AND is true only when ____ conditions are true.
The block that runs when the IF condition is false comes after the word ____.
On paper, draw the IF / ELSE as a fork in a road. Write the condition at the fork, and a message at the end of each path.

Nice decisions. Tomorrow in Beacon Lab you test two alert programs with a decision table.

← Monday