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

Wednesday

Beacon Lab: The decision table
// Booleans, conditions and alert rules
⏱ about 20 min

Wednesday: Beacon Lab: The Decision Table

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."

Your mission

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.

  • Five index cards with the readings 12, 29, 30, 31 and 75
  • A sheet of paper and a pencil
  • Two colored pencils, one for each program
  • A ruler for drawing the table
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")
}
PREDICT BEFORE YOU TEST
  • Read the question.
  • Tap your answer.
Will Programs A and B show the same message for every reading?
  1. Rule a table with three columns: Reading, Program A shows, Program B shows.
  2. Take the first card. Run Program A and write its message in one color.
  3. Run Program B on the same card and write its message in the other color.
  4. Repeat for all five cards.
  5. Circle any row where the two programs show different messages.
  6. For that row, check each condition in Program B, one at a time.
ReadingProgram A showsProgram B shows
12
29
30
31
75
CHECK YOUR DECISION TABLE
  • Read the question.
  • Tap your answer.
Reading 29. What does Program B show?
Reading 30. What does Program A show?
Reading 30. What does Program B show?
Reading 75. Do the two programs match?

Which one should Beacon use?

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 showsTrue 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.?
WHY THIS EXERCISETesting edge values in a decision table finds gaps that reading the code can miss.
A table of test inputs and the output for each one is a ____ table.
Program B's gap is at the ____ value, right on the line between its two conditions.
TEST TWO PROGRAMS FAIRLY
  • Tap a card.
  • Then tap its spot.
1First
2Second
3Third
4Last
On paper, draw a number line from 0 to 100. Color where Program B shows each message, and mark the gap.

Great lab work. Tomorrow you put decisions inside decisions.

← Tuesday