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.
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")
} 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")
} 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.
| Statement | True 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. | ? |
Sharp thinking. Tomorrow is Impact Friday: what happens when a rule is unfair.