← Back to course
2/6
Week 02 · Algorithms in Pseudocode

Tuesday

Boxes with names
// Exact steps, variables and expressions
⏱ about 20 min

Tuesday: Boxes With Names

The graph paper on Wren's clipboard is a mess. Next to Row 4 he has written 42, crossed it out, written 35, and crossed that out too.

The last number, 31, is circled.

"Which one is the water level now?" Comet asks.

"The last one," Wren says. "The others were true earlier, but they are gone now."

Nova projects a glowing box labeled waterLevel. "A variable works like that circle," she says. "It holds one value at a time."

"And when a new value arrives," Comet says, "the old one is replaced."

Nova blinks. "Let's test that idea with some pseudocode."

Variables

A variable is an abstraction inside a program that can hold a value.

Each variable holds one value at a time. That value can be a list holding many values, which you will meet in week five.

Using meaningful variable names helps people read the code and understand what each value means.

In this course, variable names are whole words joined together, like waterLevel or lightLevel.

PICK THE MOST MEANINGFUL NAME
  • Read the question.
  • Tap your answer.
A variable holds the water level of row four. Which name is clearest?
A variable holds the message Beacon sends to Priya. Which name is clearest?

Assignment

The assignment operator lets a program change the value a variable represents.

The AP Computer Science Principles reference sheet writes assignment with an arrow pointing left. Read the arrow aloud as gets.

So waterLevel ← 42 is read as: water level gets 42.

The value stored in a variable is the most recent value assigned.

waterLevel ← 42
waterLevel ← 35
waterLevel ← 31
DISPLAY(waterLevel)
TRACE THE CODE ABOVE
  • Read the question.
  • Tap your answer.
What does the program display?
After line one runs, what does waterLevel hold?
How many values does waterLevel hold at the very end?

Now a trickier one. Read each line in order, and keep track of both variables.

lightLevel ← 60
tempLevel ← 25
lightLevel ← tempLevel
tempLevel ← lightLevel
TRACE THE SWAP THAT DID NOT SWAP
  • Read the question.
  • Tap your answer.
After all four lines, what does lightLevel hold?
After all four lines, what does tempLevel hold?
What happened to the 60?

Kinds of values

Variables can hold different types of data. These types include numbers, Booleans, lists and strings.

A Boolean value is either true or false.

A string is an ordered sequence of characters, such as the message "The tomatoes need water".

A list is an ordered sequence of elements, such as a whole day of readings.

WHICH TYPE OF VALUE?
  • Read the question.
  • Tap your answer.
waterLevel gets 42.
alertText gets "The beans need water".
rowIsDry gets true.
todayReadings holds 40, 38, 35, 31 in order.
StatementTrue or false?
A variable holds the most recent value assigned to it.?
A variable can hold two separate numbers at the same time.?
The name x is as clear as waterLevel.?
A string is an ordered sequence of characters.?
WHY THIS EXERCISEMost early bugs come from losing track of what a variable holds.
Read the assignment arrow aloud as ____.
A named box that holds one value at a time is a ____.
On paper, draw three boxes labeled waterLevel, lightLevel and tempLevel. Write a value in each, then cross out and replace one.

Nice tracing. Tomorrow in Beacon Lab you trace Comet's first sensor routine line by line.

← Monday