Sequence: One Step at a Time
Programs run from top to bottom, in order. The order is everything.
You've met variables. Now let's talk about how a program actually runs. The first rule is beautifully simple.
Top to bottom, in order
Unless you tell it otherwise, a computer runs your instructions in order, from top to bottom, one line at a time. This is called sequence.
It's like reading a recipe: you do step 1, then step 2, then step 3. You don't skip ahead or jump around. Watch this little program run in your head:
cups = 2
cups = cups + 1
show cupsStep by step:
cups = 2— put 2 in the box named cups. (cups is now 2)cups = cups + 1— take what's in cups, add 1, put the result back. (cups is now 3)show cups— display the value. (shows 3)
The program displays 3. That line cups = cups + 1 might look strange — it's not math nonsense! It means "take the current value of cups, add 1, and store the new total back into cups."
Order changes the result
Because steps run in sequence, the order matters enormously. Compare these two:
price = 10
price = price + 5
show priceThis shows 15. But flip two lines:
price = 10
show price
price = price + 5This shows 10, because the display happened before the addition. Same lines, different order, different result. Just like adding salt before versus after you taste the soup.
Tip: When you're trying to understand a program, become the computer. Read it line by line, top to bottom, and keep track of what's in each box at every step. This 'playing computer' trick is one of the most powerful skills a programmer has.
Quick check
Q1 By default, in what order does a computer run a program's instructions?
Q2 What does cups = cups + 1 do?
Want to save your progress? It's free.
Create a free account