KTU S1

Sequencing

By the end you should be able to: Write pseudocode using the sequence construct, and explain why statement order matters when statements share variables.

Sequencing is the simplest construct: statements execute one after another, top to bottom, exactly once each.

BEGIN
    statement 1
    statement 2
    statement 3
END

No conditions, no repeats. It sounds trivial, and mostly is — but two points carry marks.

Order matters when statements share data

BEGIN
    a ← 5
    b ← a * 2        b is 10
    a ← 20
    PRINT b          still 10
END

b was computed from the old a. Assignment copies the value at the moment it executes; it does not create an ongoing link. Students expect b to "update", and it doesn't.

Assignment is not equality

count ← count + 1

Read as mathematics this says x=x+1x = x + 1, which is false for every number. As an instruction it means: take the current value of count, add one, store the result back in count.

The arrow ← makes the direction explicit, which is why pseudocode prefers it over =.

The classic: swapping two values

BEGIN
    READ a, b
    temp ← a
    a ← b
    b ← temp
    PRINT a, b
END

The temporary variable is essential. Without it:

    a ← b          a is now b's value
    b ← a          b gets a's NEW value — the original a is lost

Both end up holding b's original value. Tracing this by hand is a standard exam question.

Desk checking a sequence

Trace with a table — one column per variable, one row per statement:

For a=3, b=7:

Statementabtemp
READ a, b37–
temp ← a373
a ← b773
b ← temp733

Output: 7 3 ✓