Comet has rewritten the alert rule her own way. "No ELSE needed," she says proudly. "Two separate IFs. Same thing."
Wren lays his page next to the IF / ELSE version. "Are they the same? Let's investigate."
He deals out five index cards, each with one water reading: 12, 29, 30, 31 and 75.
Nova projects an empty grid above the cards. "Interesting," she says. "Test both programs with the same cards and write down every message."
"Then we will know for sure," Wren adds.
Comet crosses her arms and smiles. "Developer, prove they match."
A decision table lists test inputs and the output a program gives for each one.
You will run two alert programs on the same five readings and compare their outputs.
Then you will decide which program Beacon should use, and justify your choice.
PROGRAM A (Wren)
IF (waterLevel < 30)
{
DISPLAY("The tomatoes need water")
}
ELSE
{
DISPLAY("Water is fine")
}
PROGRAM B (Comet)
IF (waterLevel < 30)
{
DISPLAY("The tomatoes need water")
}
IF (waterLevel > 30)
{
DISPLAY("Water is fine")
} | Reading | Program A shows | Program B shows |
|---|---|---|
| 12 | ||
| 29 | ||
| 30 | ||
| 31 | ||
| 75 |
Algorithms that look similar can give different results. Program B leaves a gap at exactly 30.
Program A checks one condition per reading. Program B checks two conditions every time.
Program A also reads more clearly: either the water is low, or else it is fine.
Readability is how clear a program is to other programmers. Choosing a control structure means weighing how it is built, how readable it is and how it performs.
| What the lab shows | True or false? |
|---|---|
| Programs A and B gave different output for a reading of 30. | ? |
| Program A always shows exactly one message. | ? |
| Program B checks fewer conditions than Program A. | ? |
| If Program B's second condition were waterLevel ≥ 30, the two programs would match for every reading. | ? |
Great lab work. Tomorrow you put decisions inside decisions.