Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Sequence

Executing a series of instructions one after another in a fixed, specific order.


Quick Answer

Executing a series of instructions one after another in a fixed, specific order. You rely on it whenever you read or write code, because the computer runs line 1, then line 2, then line 3, each finishing before the next begins. The order is part of the meaning: x = 5 followed by x = x * 2 leaves x at 10, while the same two lines swapped either fail or leave x at 5. Tracing a program line by line, with a table of each variable's value, is how you predict what it will output.

Why This Matters

Many bugs in beginner programs are sequence bugs: using a value before it is set, printing a total before the last item is added, or updating a score after the screen has already shown it. Once you trace line by line, you can predict exactly what a program prints and find the line that is in the wrong place. Without that habit, code looks like a pile of statements that should somehow all happen at once.

Intuitive Explanation

A computer reads a program the way you would follow a recipe card if you were not allowed to skip around: one instruction, completely, then the next. Take this program: price = 4; count = 3; total = price * count; count = 5; print(total). Line 3 computes 4 * 3 = 12 and stores 12 in total. Line 4 changes count to 5, but total does not recompute itself; it keeps the 12 it was given. The program prints 12.

Each line uses the values that exist at the moment it runs. A trace table makes this visible: one column per variable, one row per line, filled in as you go. After line 2: price 4, count 3. After line 3: total 12. After line 4: count 5, while total is still 12. Reading the finished table from the bottom row tells you exactly what print will show.

Contrast this with a spreadsheet, where a cell holding =A1*B1 updates by itself whenever A1 or B1 changes. Program variables are not formulas; they hold whatever was last stored in them. Sequence also differs from the other control structures: in pure sequence every line runs exactly once, top to bottom. If a line might be skipped, you are looking at selection; if a line runs again, you are looking at iteration.

Think of it like this. Sequence is like a row of dominoes: each one falls only after the one before it, and you cannot make the fifth fall first. Move one domino to a later spot and everything after it happens differently. Unlike dominoes, a program can later jump back or skip ahead, but that takes selection or loops.

Core idea. Each step must complete fully before the next step begins; the order determines the result.


When to Use

Use sequence when you must trace code that runs top to bottom, predict its output, or find a statement in the wrong order, such as a value used before it is set or a print placed before an update. Record every variable after each line. If the code contains if or else, you also need Selection to decide which lines run; if it contains for or while, you need Iteration to count how many times a block repeats.

Tip. Ask: Does every line run once, top to bottom, with the answer depending on their order?

How to Recognize It

Before treating code as a plain sequence, scan it for anything that makes lines get skipped or run again.

  1. Does each line run exactly once, from top to bottom?

    Yes means you can trace it as a sequence. If some lines run only when a condition is true, that is Selection; if some run many times, that is Iteration.

  2. Does a later line use a value that an earlier line created or changed?

    Yes means order matters, and swapping those lines changes the result. No means those two lines are independent and could be swapped safely.

  3. Is the question asking what a variable holds after the last line?

    Yes means trace line by line with a table of values. If it asks for a whole method that solves a problem for any input, think Algorithm.

Sequence vs Algorithm vs Selection vs Iteration

Every program uses sequence, so it hides inside the other three. Ask whether each line runs once in the written order, or whether some lines are chosen, skipped or repeated.

Sequence

Meaning
Sequence runs each instruction once, in the order written, with each one finishing before the next starts.
Key test
Every line runs once, top to bottom, and order changes the result.
Formula
S1; S2; …; Sn
Example
x = 2, then x = x + 3, then x = x * 4 leaves x at 20.

Algorithm

Meaning
An algorithm is a complete method for a task, which may use sequence, choices and repeats.
Key test
The whole solution method, not just the order of its lines.
Formula
output=f(input)
Example
A method for finding the largest number in any list, however long it is.

Selection

Meaning
Selection uses a true-or-false condition to decide which block of code runs.
Key test
Some lines run only when a condition is true.
Example
IF the door sensor reads open THEN beep twice ELSE stay quiet.

Iteration

Meaning
Iteration repeats a block of code until a count is reached or a condition says stop.
Key test
Some lines run more than once.
Example
REPEAT 3 TIMES: add 5 to total, so total grows by 15.

Formula & Notation

Worked Examples

Example 1 — Trace a five-line program

Easy

What does this program print? a = 2; b = a + 3; a = b * 2; b = a - 1; print(a, b)
Show the steps and the answer
  1. Line 1: a = 2.

    This is the first value stored in a.

  2. Line 2: b = a + 3 = 2 + 3 = 5.

    a holds 2 at this moment.

  3. Line 3: a = b * 2 = 5 * 2 = 10.

    This replaces the old value of a; b is still 5.

  4. Line 4: b = a - 1 = 10 - 1 = 9.

    Line 4 uses the new a, 10, because line 3 has already finished.

  5. Line 5 prints 10 9.

    print shows the values left after all the earlier lines have run.

Answer: 10 9

Takeaway: Keep a running table of every variable, and update it after each line before you read the next one.

Example 2 — Same lines, different order

Standard

Both programs contain the same four lines in a different order. What does each print? A: price = 50; price = price - 5; tax = price * 0.1; print(price + tax). B: price = 50; tax = price * 0.1; price = price - 5; print(price + tax).
Show the steps and the answer
  1. Trace A: price = 50, then price = 45, then tax = 45 * 0.1 = 4.5.

    In A the discount line runs before the tax line, so tax is worked out on 45.

  2. A prints 45 + 4.5 = 49.5.

    Both values are read after the three earlier lines have run.

  3. Trace B: price = 50, then tax = 50 * 0.1 = 5, then price = 45.

    In B the tax line runs while price is still 50, and changing price afterwards does not change tax.

  4. B prints 45 + 5 = 50.

    The total differs from A's by 0.5 even though not a single line changed.

Answer: A prints 49.5; B prints 50 (shown as 50.0 in Python).

Takeaway: Before reordering lines, check whether a later line uses a value that an earlier line changes.

Example 3 — Find the mistake: using values before they exist

Application

Theo wants to print the area of a 6-by-4 rectangle. He writes: area = width * height; width = 6; height = 4; print(area). The program stops with an error on the first line. Find the mistake and correct it.
Show the steps and the answer
  1. Trace line 1: area = width * height.

    width and height have not been given values yet, so there is nothing to multiply, and most languages stop with an error.

  2. Name the error: the line that uses width and height comes before the lines that set them.

    The computer does not read ahead to find later assignments; it knows only what has already run.

  3. Fix the order: width = 6; height = 4; area = width * height; print(area).

    Now both values exist when the multiplication runs.

  4. Trace the fix: area = 6 * 4 = 24, so it prints 24.

    Every line now uses only values set by the lines above it.

Answer: Move area = width * height below the two assignments; the program then prints 24.

Takeaway: Set every variable before the first line that reads it.

Where You'll See It

  • A morning routine

    You must wake up, then get dressed, then eat breakfast in order; you can't eat breakfast before waking up, which shows that order determines the result.

  • Following LEGO build instructions

    Each numbered step must be finished before the next, and doing steps out of order leaves you with a model that won't fit together.

  • Baking, then frosting a cake

    You can't frost the cake before it's baked and cooled; the steps run one at a time in a fixed order, which is exactly what sequence means.

Common Mistakes

  1. The slip: I can write the line that uses a value before the line that creates it, since they're in the same program.

    Why it's tempting: Reading the whole program at once, all the lines feel 'present' together.

    Instead: Statements run top to bottom one at a time; you must set (initialize) a value before any line that reads it, or it won't exist yet.

  2. The slip: The order of independent-looking lines doesn't really matter.

    Why it's tempting: If two lines look unrelated, swapping them seems harmless.

    Instead: Before reordering, check whether a later step depends on an earlier step's result; if it does, changing the order changes the output.

  3. The slip: The computer runs all my instructions at the same time.

    Why it's tempting: Programs feel instant, so it seems like everything happens at once.

    Instead: In sequence, each instruction completes fully before the next begins; they execute one after another, not simultaneously.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. Trace this program: `x = 4`, then `x = x * 3`, then `x = x - 5`, then `print(x)`. What is printed?

    Hint: Run each line in order and write down `x` after each one.

  2. Trace: `a = 2`, then `b = a + 5`, then `a = 10`, then `print(b)`. What is printed?

    Hint: What was `a` at the moment `b` was set?

  3. Trace this swap: `a = 3`, `b = 8`, `temp = a`, `a = b`, `b = temp`. What are `a` and `b` at the end?

    Hint: Write all three variables after each line.

  4. Another student swaps with only two lines, `a = b` then `b = a`, starting from `a = 3` and `b = 8`. What are the final values?

    Hint: After the first line runs, is the 3 still stored anywhere?

  5. A program must print "Too cold" only when the temperature is below 10. Is a plain sequence of steps enough, or do you need selection?

    Hint: Does a sequence ever skip a line?

  6. A student wants the tax on a 12-dollar price and writes: `price = 8`, `tax = price * 0.25`, `price = 12`, `print(tax)`. They expect 3. What is printed, and how do they fix it?

    Hint: What was `price` when the tax line ran?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

If line 5 of my program has an error, do lines 1 to 4 still run?

It depends on the kind of error. Python reads the whole file before running it, so a mistake in how the code is written, such as a missing bracket, stops everything before line 1 runs. But an error that only appears while running, such as dividing by zero on line 5, happens after lines 1 to 4 have done their work: anything they printed stays on screen, and nothing after line 5 runs.

Do computers really run one line at a time?

From your program's point of view, yes. Modern processors use tricks to do several things at once, such as starting the next instruction early or running separate programs on separate cores, but they are designed so the result is the same as if each line of your program ran in order. That is why tracing line by line still predicts the output correctly.

Is a sequence the same in Scratch and in Python?

Mostly. In Scratch, blocks snapped under each other run from top to bottom, the same as lines in Python. One difference catches people out: two separate Scratch scripts that both start with 'when green flag clicked' are not one sequence. They run side by side, so you cannot rely on one script finishing before the other begins.

Learning Path

← Before

Algorithm
Sequence

You are here

You need Algorithm first: a task written as exact steps. Sequence adds the rule that a computer runs those steps one at a time, in order, each using the values left behind by earlier steps. Next, Selection lets a program choose which steps to run, and Iteration lets it repeat steps; both are traced with the same line-by-line method.

See Also