6. Variables¶
A variable is a labeled box that holds a value which can change while the program runs. Think of it as a name taped to a storage bin — you can look inside, and you can swap out what's there.
Pseudocode sets a variable with SET ... TO ...:
SET score TO 0
SET score TO score + 1
The second line reads the current value of score, adds 1, and stores the result back into score. In a flowchart, this is just a process rectangle that happens to be an assignment:
[ SET score TO score + 1 ]
Combined with a loop, variables let you count things:
SET count TO 0
WHILE count is less than 5 DO
SET count TO count + 1
Try it
Write pseudocode that counts from 1 to 5 using a variable and a loop, the way the example above does.
Tip
A variable can only hold one value at a time — setting it again overwrites whatever was there before.