← Back to course
Intro to CS 9-10 / Week 03 / Thursday
4/6
Week 03 · Making Decisions

Thursday

Decisions inside decisions
// Booleans, conditions and alert rules
⏱ about 20 min

Thursday: Decisions Inside Decisions

Tomas brings a new request. "When the water is low and the grow lights are very strong, the tomatoes dry out faster."

"So Beacon should say water them soon," Comet says. "And just the normal alert when the lights are gentle."

She starts writing an IF inside another IF. Then she stops. "Wait. Could I use AND instead?"

Wren tilts his head. "What do you notice? Both ways ask the same two questions."

Nova hovers above the two drafts. "Let's debug together," she says. "Trace both with the same readings and compare."

"Then pick the one Lena could read at a glance," Wren adds.

Nested conditionals

An if-statement can sit inside another if-statement. This is called a nested conditional.

The inside IF is only checked when the outside condition sends the program down that path.

In this course, each level is indented so the nesting is easy to see.

IF (waterLevel < 30)
{
   IF (lightLevel > 80)
   {
      DISPLAY("Water the tomatoes soon")
   }
   ELSE
   {
      DISPLAY("The tomatoes need water")
   }
}
ELSE
{
   DISPLAY("Water is fine")
}
TRACE THE NESTED CONDITIONAL
  • Read the question.
  • Tap your answer.
waterLevel is 20 and lightLevel is 90. What does Beacon show?
waterLevel is 20 and lightLevel is 50. What does Beacon show?
waterLevel is 60 and lightLevel is 95. What does Beacon show?

One decision, two ways

Algorithms can be written in different ways and still accomplish the same task.

A nested IF with no ELSE asks two questions, one after the other. AND asks both questions at once.

Because AND is true only when both conditions are true, these two versions always show the same output.

VERSION 1: NESTED
IF (waterLevel < 30)
{
   IF (lightLevel > 80)
   {
      DISPLAY("Water the tomatoes soon")
   }
}

VERSION 2: AND
IF (waterLevel < 30 AND lightLevel > 80)
{
   DISPLAY("Water the tomatoes soon")
}
COMPARE THE TWO VERSIONS
  • Read the question.
  • Tap your answer.
waterLevel is 25 and lightLevel is 85. Do the versions match?
waterLevel is 25 and lightLevel is 60. Do the versions match?
Which version would Lena likely read faster?

Readability

Readability is how clear a program is to other programmers. It can be improved through documentation.

At this level, control structures include conditional statements, loops, event handlers and recursion.

A good developer can explain why one structure was chosen over another, including its drawbacks.

StatementTrue or false?
In a nested conditional, the inside IF is always checked.?
A nested IF with no ELSE can do the same job as AND.?
Two algorithms written differently can accomplish the same task.?
Readability does not matter as long as the program works.?
WHY THIS EXERCISEChoosing the clearest correct structure makes Beacon easier for the whole team to check.
HOW BEACON RUNS THE NESTED CONDITIONAL
  • Tap a card.
  • Then tap its spot.
1First
2Second
3Third
4Last
An IF inside another IF is a ____ conditional.
How clear a program is to other programmers is its ____.
On paper, draw the nested conditional as a tree. Draw one fork for water, then a fork for light on the low-water branch.

Sharp thinking. Tomorrow is Impact Friday: what happens when a rule is unfair.

← Wednesday