MODULE 2 • LESSON 06
Every program you will ever write is built from a surprisingly small set of parts. Data comes in (input), values are worked out and stored (assignment), results go out (output), and the steps are carried out in some order. The simplest order is one after another, from top to bottom: this is called sequence.
This is the first of three lessons on the constructs of structured programming. Here you will study sequence, input and output statements, and the assignment statement, which together make up most lines of any program. Lesson 07 adds selection (decisions) and Lesson 08 adds iteration (loops). You will watch values move in and out of memory boxes, repair programs whose lines are in the wrong order, and translate every example into C.
What you will learn
- What structured programming is, and the three control structures it uses.
- What variables and constants are, and why they have data types.
- How to write input and output statements in pseudocode and in C.
- What an assignment statement does, step by step, including counters and running totals.
- Why the order of statements in a sequence matters.
- How to design, trace and code a complete sequence algorithm.
Part 1: Structured programming and its constructs
Structured programming is a way of writing algorithms and programs using only a few well-understood building blocks, each with one way in and one way out. Programs written this way are easier to read, test and change than programs that jump around with “go to” statements. A famous result in computer science (the Böhm–Jacopini theorem, 1966) showed that any algorithm can be written using just three control structures:
| Control structure | What it does | Pseudocode | C |
| Sequence | Carries out steps one after another, in order | one statement per line | statements ending in ; |
| Selection | Chooses between paths depending on a condition | IF … THEN … ELSE … ENDIF | if ( ) { } else { } |
| Iteration (repetition) | Repeats steps, a fixed number of times or until a condition changes | FOR, WHILE, REPEAT … UNTIL | for, while, do … while |
Inside those structures, the individual steps are mostly input statements, output statements and assignment statements. The syllabus lists all of these together as the constructs you must be able to explain. When an exam asks you to “state the constructs used in structured programming”, sequence, selection and iteration are the three control structures; a fuller answer also mentions input/output and assignment.
Part 2: Variables, constants and data types
Before we can input or assign anything, we need somewhere to keep it. A variable is a named location in memory that holds a value which can change while the program runs. Think of it as a labelled box: the label (name) stays the same, but the contents can be replaced.
A constant is a named value that does not change while the program runs, such as VAT_RATE ← 0.125 or SEATS_PER_BUS ← 40. Using a constant instead of typing 0.125 in ten places means that if the rate changes, you change one line, and the name explains what the number means.
Every variable has a data type, which decides what kind of value it can hold and what can be done with it:
| Data type | Holds | Examples | In C |
| Integer | Whole numbers | age, count, quantity | int |
| Real (floating point) | Numbers with a fractional part | price, average, temperature | float, double |
| Character | A single symbol | grade, answer (Y/N) | char |
| String | Text | name, address | char array (see the Strings post) |
| Boolean | True or False | found, isValid | int (0 or 1), or bool with stdbool.h |
Pseudocode usually does not declare types, but you should still think about them. C insists: every variable must be declared with its type before it is used, for example int count; or float price;.
Naming tips. Use meaningful names (total, not t), start with a letter, no spaces (totalMarks or total_marks), and do not reuse a name for two different things.
Part 3: Input statements
An input statement takes a value from outside the program (usually the keyboard, but it could be a file, a scanner or a sensor) and stores it in a variable. In pseudocode we write INPUT or READ followed by the variable name:
INPUT age READ name INPUT length, width
Notice that the variable is what receives the value. After INPUT age runs and the user types 16, the box called age holds 16; whatever was in it before is gone.
Prompts. A user cannot guess what to type. A good program first outputs a prompt such as “Enter your age: ” and then inputs the value. In pseudocode you may write the prompt as a separate OUTPUT, or combine them: INPUT "Enter your age: ", age. Either is fine if you are consistent.
In C, input from the keyboard uses scanf. The format specifier says what type to expect, and the & before the variable name tells scanf where in memory to put the value:
printf("Enter your age: ");
scanf("%d", &age); /* int */
scanf("%f", &price); /* float */
scanf("%d %d", &length, &width);
Forgetting the & is one of the most common beginner errors in C: the program compiles (sometimes with a warning) but crashes or stores the value in the wrong place.
Part 4: Output statements
An output statement sends information out of the program, usually to the screen (or a printer or file). In pseudocode use OUTPUT, PRINT or DISPLAY. An output can contain literal text in quotation marks, the value of a variable, or both:
| Pseudocode | If total holds 65, the screen shows |
| OUTPUT total | 65 |
| OUTPUT “total” | total |
| OUTPUT “Total: “, total | Total: 65 |
| OUTPUT “Total: total” | Total: total |
The difference between the first two rows matters in exams: text inside quotation marks is printed exactly as written, while a name without quotation marks is replaced by the value stored in that variable. A common error is to put a variable name inside the quotes, or to forget the closing quote.
In C, output uses printf. Literal text goes in the format string, and each %d (int) or %f (float) is replaced by the next value in the list. \n moves to a new line, and %.2f shows two decimal places:
printf("Total: %d\n", total);
printf("Average: %.2f\n", avg);
printf("%d x %d = %d\n", a, b, a * b);
Part 5: The assignment statement
An assignment statement works out the value of an expression and stores the result in a variable, replacing what was there before. In pseudocode we usually use a left arrow: area ← length * width, read as “area becomes length times width” or “area is assigned length times width”. Some pseudocode and C use =.
An assignment always happens in two stages:
- Evaluate the right-hand side, using the values the variables hold at that moment.
- Store the result in the variable on the left-hand side, overwriting its old value.
This explains some statements that look strange if you read = as “equals” in the maths sense:
| Statement | Meaning | Name |
| count ← count + 1 | Take the old count, add 1, store it back in count | Counter (increment) |
| total ← total + price | Take the old total, add the price, store it back | Accumulator (running total) |
| total ← 0 | Give total a starting value before using it | Initialisation |
| x ← y | Copy the value of y into x (y is unchanged) | Copy |
Two rules follow. The left-hand side must be a single variable: a + b ← c is meaningless. And the right-hand side can be any expression built from values, variables and operators.
Operators in expressions
| Operator | Meaning | Example | Result |
| + – * / | Add, subtract, multiply, divide | 7 / 2 | 3.5 (in pseudocode) |
| DIV | Whole-number division (quotient) | 17 DIV 5 | 3 |
| MOD | Remainder after division | 17 MOD 5 | 2 |
| ( ) | Brackets are worked out first | 2 * (3 + 4) | 14 |
Normal maths precedence applies: brackets first, then * and /, then + and -. So 2 + 3 * 4 is 14, not 20. In C, DIV is just / between two ints (so 17 / 5 is 3), and MOD is % (so 17 % 5 is 2). Dividing two integers in C throws away the fraction, which is a frequent source of wrong averages, as the program in Part 7 shows.
C notes. In C the assignment operator is =, and the comparison “is equal to” is ==. Mixing them up (if (x = 5)) is a classic bug. C also has shorthand: count++ means count = count + 1, and total += price means total = total + price.
Try it 1Memory boxes: watch assignment happen
Choose a program and tap Step to run one line at a time. Each variable is a box in memory. The box that has just changed is outlined in orange, and its old value is shown crossed out. The trace table below fills in as you go. Predict the result of Swap (wrong) before you run it.
The wrong swap is worth remembering. Once a ← b has run, the original value of a is gone for ever, because assignment overwrites. The fix is a third, temporary variable that keeps a copy. You will use this three-line swap again when you sort lists later in the course.
Part 6: Sequence
Sequence means the statements are carried out one after another, in the order they are written, each exactly once. A sequence algorithm has no decisions and no loops: control simply flows from the first statement to the last. In a flowchart, a sequence is a straight chain of symbols joined by arrows.
Even without decisions or loops, order matters. A statement can only use a value that an earlier statement has produced. The usual shape of a sequence is:
- Input (or initialise) the values needed.
- Process: work out the results with assignment statements.
- Output the results.
If you swap two lines so that a value is used before it is set, the algorithm is wrong even though every individual line is correct. In some languages the program would crash; in C it would use whatever "garbage" value happened to be in memory, which is worse, because it may look like it works.
Try it 2Fix the sequence
The lines of each algorithm are in the wrong order. Use the ↑ and ↓ buttons to move lines, then tap Run. The checker stops at the first line that uses a variable before it has a value, and tells you which one.
For the rectangle there is more than one correct order: the area and perimeter lines can be in either order, as long as both come after the INPUT and before the OUTPUT that uses them. Algorithms often have this kind of freedom. What is fixed is the dependency: a value must be produced before it is used.
Part 7: A complete sequence algorithm
Problem. A tiler needs the area and the perimeter of a rectangular floor, given its length and width in whole metres.
1. Narrative
Ask for the length and the width. Multiply them to get the area. Add them and double the result to get the perimeter. Display the area and the perimeter, each with a label.
2. Pseudocode
OUTPUT "Length: " INPUT length OUTPUT "Width: " INPUT width area ← length * width perim ← 2 * (length + width) OUTPUT "Area: ", area OUTPUT "Perimeter: ", perim
3. Flowchart
4. Trace table
With length 6 and width 4:
| Line | length | width | area | perim | Output |
| INPUT length | 6 | ||||
| INPUT width | 6 | 4 | |||
| area ← … | 6 | 4 | 24 | ||
| perim ← … | 6 | 4 | 24 | 20 | |
| OUTPUT area | Area: 24 | ||||
| OUTPUT perim | Perimeter: 20 |
5. Program (C)
#include <stdio.h>
int main(void)
{
int length, width, area, perim;
printf("Length: ");
scanf("%d", &length);
printf("Width: ");
scanf("%d", &width);
area = length * width;
perim = 2 * (length + width);
printf("\nArea: %d\n", area);
printf("Perimeter: %d\n", perim);
return 0;
}
Sample input: 6 4
Output:
Length: Width: Area: 24 Perimeter: 20
Each pseudocode line has become one C statement. The extra lines at the top declare the four variables as int, because C must know each variable’s type before it is used.
Two more short programs
The correct swap from Try it 1, in C:
#include <stdio.h>
int main(void)
{
int a = 5, b = 8, temp;
printf("Before: a = %d, b = %d\n",
a, b);
temp = a; /* keep a copy of a */
a = b; /* a now holds 8 */
b = temp; /* b gets the old a */
printf("After: a = %d, b = %d\n",
a, b);
return 0;
}
Output:
Before: a = 5, b = 8 After: a = 8, b = 5
Integer division in an assignment. With marks 7, 8 and 8 the total is 23, and the true average is 7.67. Dividing an int by the int 3 throws away the fraction; dividing by 3.0f keeps it:
#include <stdio.h>
int main(void)
{
int m1, m2, m3, total;
int avgInt;
float avgReal;
printf("Three marks: ");
scanf("%d %d %d", &m1, &m2, &m3);
total = m1 + m2 + m3;
avgInt = total / 3; /* int / int */
avgReal = total / 3.0f; /* real */
printf("\nTotal: %d\n", total);
printf("total / 3 = %d\n", avgInt);
printf("total / 3.0 = %.2f\n", avgReal);
return 0;
}
Sample input: 7 8 8
Output:
Three marks: Total: 23 total / 3 = 7 total / 3.0 = 7.67
This is why the data type of a variable, and of the values in an expression, matters even in a three-line sequence.
Try it 3Statements practice
Pick a mode. "What kind?" asks you to classify a statement. "Final value" gives a short sequence of assignments: type the final value of the variable. Tap New question for another.
Summary
| Idea | Remember |
| Structured programming | Programs built from sequence, selection and iteration, each with one entry and one exit. |
| Variable / constant | Named memory location whose value can change / cannot change while the program runs. Each has a data type. |
| Input | INPUT or READ a value into a variable. C: scanf with a format specifier and &. |
| Output | OUTPUT, PRINT or DISPLAY text in quotes and/or variable values. C: printf. |
| Assignment | Evaluate the right-hand side, then store it in the left-hand variable, overwriting it. C uses =. |
| Sequence | Statements run once each, in order. A value must be set before it is used. |
Review questions
1. Name the THREE control structures of structured programming and give a pseudocode keyword for each of the last two.
Show answer
Sequence; selection (IF … THEN … ELSE); iteration or repetition (FOR, WHILE or REPEAT … UNTIL).
2. Explain the purpose of an assignment statement, using an example.
Show answer
An assignment statement evaluates an expression and stores the result in a variable, replacing its old value. Example: area ← length * width works out length times width and stores the answer in area.
3. Distinguish between a variable and a constant.
Show answer
Both are named values. A variable’s value can change while the program runs (e.g. total); a constant’s value is fixed for the whole run (e.g. VAT_RATE ← 0.125).
4. What is the output of: x ← 4; y ← x * 3; x ← y - x; OUTPUT x, y?
Show answer
y = 12, then x = 12 - 4 = 8. Output: 8 12.
5. State the difference between OUTPUT total and OUTPUT "total".
Show answer
The first displays the value stored in the variable total (e.g. 65). The second displays the word total, because text in quotation marks is printed exactly as written.
6. Why does this sequence not work, and how should it be fixed? INPUT price; total ← price * qty; INPUT qty; OUTPUT total
Show answer
qty is used before it has been input, so total is calculated with no (or a garbage) value for qty. Move INPUT qty above the assignment.
7. Explain why count ← count + 1 is not a contradiction.
Show answer
← is not "equals". The right-hand side is evaluated first using the old value of count, then the result is stored back in count, so count increases by 1.
8. In C, why does avg = total / 3; give 7 when total is 23 and avg is a float?
Show answer
total and 3 are both int, so C performs integer division (23 / 3 = 7) before the result is stored in avg. Use total / 3.0 (or a cast) to get 7.67.
Homework
- Write pseudocode, a flowchart and a C program that input a price in dollars and a quantity, and output the subtotal, 12.5% VAT and the total. Use a constant for the VAT rate.
- Trace this sequence with a trace table and give the output:
a ← 3; b ← a + 4; a ← b * a; b ← a MOD 5; OUTPUT a, b. - Write a sequence that inputs a number of minutes and outputs it as hours and minutes (e.g. 135 → 2 h 15 min). Hint: DIV and MOD.
- Rewrite the wrong swap from Try it 1 so that it works without a temp variable, using only + and -. Trace it with a = 5, b = 8.
- In
rectangle.c, change the types tofloatand the specifiers to%fso that it accepts 2.5 m. What else must change?
Mastery quiz: Sequence, Input, Output and Assignment
This 10-question quiz is separate from the short Quick quiz at the end of the lesson. Score 8/10 or higher to earn this lesson’s badge. Your best score is saved in this browser on this device.
Mastery quiz: Sequence, Input, Output and Assignment
Your course badges: loading saved progress…
View badges
No course badges earned yet. Score 8/10 in a lesson quiz to earn its badge.
Saved in this browser on this device. Clearing browser data may remove your badges.
Answer all 10 questions. Earn this lesson’s badge at 8/10 or higher. Read the explanations, revisit the Try it activities and retry as often as you need. These are self-study badges, not formal grades.
Exam tips
Specimen Unit 1 Paper 01 (2021), Item 22. A multiple-choice item on the purpose of an assignment statement. The key words are "evaluate an expression" and "store the result in a variable".
Specimen Unit 1 Paper 01 (2021), Item 24. Spotting the faulty line in a short input-process-output sequence. Check every OUTPUT for correctly placed quotation marks (is it printing text or a variable?) and every assignment for the right variable names and operators.
Specimen Unit 1 Paper 02 (2021), Question 3(b); CAPE 2024 Unit 1 Paper 02, Question 4(b); CAPE 2009 Unit 1 Paper 02, Question 3(b). Naming the constructs of structured programming. Give sequence, selection and iteration, and add a one-line description or keyword for each if the question asks you to "describe". Worked answers for 2024 and 2009 are linked in the Go further box.
Trace tables (any year). When an assignment overwrites a variable, write the new value in a new row; never cross out and squeeze. Markers look for each change in order.
More worked answers are on the CS Solutions page.
Previous: Module 2 • Lesson 05 – Representing Algorithms: Narrative, Flowcharts and Pseudocode. Next: Module 2 • Lesson 07 – Selection: IF-THEN, IF-THEN-ELSE and Nested IF.
Syllabus link: CAPE Computer Science (CXC A19/U2/21), Problem-Solving with Computers (Unit 1, Module 2). Specific Objective 6: explain constructs used in structured programming (input and output statements; the control structure sequencing; the assignment statement).
Video search terms
Search YouTube for: "assignment statement pseudocode", "swap two variables temp", "scanf and printf in C for beginners", "integer division in C".
Try it 4Quick quiz
Tap an answer. All eight questions are self-checks on this lesson.
Updates
2026/12/01
- Updated the copyright line.
- Published. Previous/Next links and the mastery quiz links now use the full lesson addresses.
- Small script tidy-up for the site (no change to how the activities work).
2026/11/24
- Linked Previous/Next and the mastery quiz URLs to draft shortlinks (?p=ID), which will redirect to the permalink after publish.
- First version of the lesson.
© 2026 islandclass.org
