Objectives: Students should be able to —
- 1 Declare and use variables and constants in pseudocode.
- 2 Identify and use the data types INTEGER, REAL, CHAR, STRING and BOOLEAN.
- 3 Understand the three programming constructs: sequence, selection and iteration.
- 4 Use IF…THEN…ELSE and nested IF statements.
- 5 Use CASE…OF selection statements.
- 6 Use FOR…NEXT, WHILE…DO and REPEAT…UNTIL loops.
- 7 Use arithmetic operators: + , − , ∗ , / , MOD , DIV , ^.
- 8 Use relational operators: = , ≠ , < , > , ≤ , ≥.
- 9 Use Boolean operators: AND, OR, NOT.
- 10 Use INPUT and OUTPUT statements.
- 11 Use counters and accumulators (totalling variables).
- 12 Understand the use of procedures and functions in modular programming.
Variables, Constants & Data Types
Variables and Constants
A variable is a named storage location in the computer's memory whose value can change during the execution of a program.
Each variable has a name (identifier) and a data type that determines what kind of data it can hold.
Example (pseudocode):
Here, Age is the variable name, INTEGER is its data type, and the value 15 is assigned to it using the assignment operator ←.
A constant is a named value that does not change during the execution of a program. It is declared once and its value remains fixed throughout.
Reasons for using a constant:
- The value is fixed and should not be accidentally altered by the program (e.g. Pi = 3.14159, VAT = 0.05).
- It makes the program more readable and easier to maintain — if the value ever needs to change, the programmer only edits it in one place.
Example (pseudocode):
| Variable | Constant |
|---|---|
| Value can change during program execution | Value remains fixed throughout the program |
| Declared with DECLARE and assigned with ← | Declared with CONSTANT keyword |
| Used for data input by the user, counters, totals | Used for fixed values like Pi, tax rates, conversion factors |
| Example: Age ← 15 | Example: CONSTANT Pi = 3.14159 |
Example 1: CONSTANT Pi = 3.14159
Pi is a mathematical constant — its value never changes. Storing it as a constant prevents accidental modification and makes the code clearer.
Example 2: CONSTANT VAT_RATE = 0.05
The VAT (Value Added Tax) rate is fixed for the duration of the program. If the law changes, the programmer updates it once at the declaration, and every calculation that uses VAT_RATE is automatically updated.
Data Types
| Data Type | Description | Example |
|---|---|---|
| INTEGER | A whole number (positive, negative or zero) | 42, −7, 0 |
| REAL | A number with a fractional (decimal) part | 3.14, −0.5, 9.81 |
| CHAR | A single character (letter, digit or symbol) | 'A', '7', '?' |
| STRING | A sequence of zero or more characters | "Hello", "CS0478" |
| BOOLEAN | A logical value: either TRUE or FALSE | TRUE, FALSE |
(a) Person's age in years:
INTEGER — age is always a whole number (e.g. 15, 16).
(b) Price of an item in dollars and cents:
REAL — prices can have a decimal part (e.g. 9.99, 4.50).
(c) Person's full name:
STRING — a name is a sequence of characters (e.g. "Shyam Subrahmanya").
(d) Single-letter initial of a first name:
CHAR — only one character is needed (e.g. 'S').
(e) Whether the user has logged in or not:
BOOLEAN — there are only two possible states: TRUE (logged in) or FALSE (not logged in).
- Memory efficiency: different data types use different amounts of memory. Choosing the smallest adequate type (e.g. INTEGER instead of REAL for an age) saves memory.
- Correct operations: arithmetic operators like DIV and MOD only work on numeric types. Boolean operators (AND / OR / NOT) only work on BOOLEAN values.
- Data validation: declaring a variable as INTEGER prevents a string like "abc" from being stored in it, reducing runtime errors.
- Avoiding truncation/rounding errors: storing 3.14 in an INTEGER variable would lose the fractional part and store only 3.
- Program clarity: the data type tells another reader what kind of value the variable holds, making the code easier to understand and maintain.
Pseudocode:
Output:
Sequence, Selection & Iteration
The Three Programming Constructs
| Construct | Definition | Example |
|---|---|---|
| Sequence | Instructions are executed one after another, in the order they are written. | INPUT X Y ← X + 5 OUTPUT Y |
| Selection | A condition is tested; the program chooses which path of instructions to execute. | IF X > 10 THEN OUTPUT "Big" ELSE OUTPUT "Small" |
| Iteration | A block of instructions is repeated a number of times (also called a loop). | FOR I ← 1 TO 5 OUTPUT I NEXT I |
Every program — no matter how complex — is built using only these three constructs. This is sometimes called the three basic constructs principle.
Selection: IF and CASE Statements
An IF…THEN…ELSE statement evaluates a condition. If the condition is TRUE, the instructions after THEN are executed. If FALSE, the instructions after ELSE are executed instead.
Pseudocode:
Sample trace:
- If Mark = 65 → condition TRUE → output "Pass".
- If Mark = 30 → condition FALSE → output "Fail".
A nested IF statement is an IF statement placed inside another IF statement. It is used when there are multiple conditions to test in sequence, each producing a different outcome.
Pseudocode:
Each inner IF is only reached if the outer condition was FALSE. This is the standard pattern for handling multi-way selection when the conditions form a range.
A CASE…OF statement is a multi-way selection structure that chooses one of several paths based on the value of a single variable or expression. It is cleaner than a long chain of nested IFs when there are many distinct discrete values.
Pseudocode:
The OTHERWISE clause runs if none of the listed values match — useful for handling invalid input.
| Feature | IF…THEN…ELSE | CASE…OF |
|---|---|---|
| Tests | A Boolean condition (e.g. X > 50) | Discrete values of one variable (e.g. 1, 2, 3) |
| Range testing | Naturally supports ranges (e.g. Mark >= 75) | Each value must be listed separately — ranges are awkward |
| Readability | Becomes clumsy with many conditions (deeply nested) | Very clean for many discrete values |
| Compound conditions | Supports AND / OR / NOT in conditions | Tests only one variable at a time |
| Default path | ELSE clause | OTHERWISE clause |
Rule of thumb: use IF for ranges and compound conditions, use CASE when testing one variable against many fixed, distinct values.
Iteration: FOR, WHILE and REPEAT Loops
A FOR…NEXT loop is a count-controlled loop. It repeats a block of instructions a fixed, known number of times, using a loop counter that automatically increases (or decreases) by a step value each time.
Pseudocode:
Output:
The loop runs exactly 5 times — once each for N = 1, 2, 3, 4, 5. After N = 5, control passes to the statement after NEXT N.
A WHILE…DO loop is a condition-controlled loop. The condition is tested before each iteration. If TRUE, the loop body executes; if FALSE, the loop terminates immediately.
Because the test is at the start, the body may run zero or more times — if the condition is initially FALSE, the loop body never runs at all.
Pseudocode:
Sample trace (input: 5, 3, 7, 0):
A REPEAT…UNTIL loop is a condition-controlled loop where the condition is tested after each iteration. The loop body runs first, then the condition is checked — if FALSE, the loop runs again; if TRUE, it terminates.
Because the test is at the end, the body always runs at least once. Note: the loop continues while the condition is FALSE and stops when the condition becomes TRUE.
Pseudocode:
The body executes at least once, so even if the user's first input is "secret123" the prompt is shown once. If the user enters "abc" (too short), the loop repeats until a valid password is supplied.
| Feature | FOR…NEXT | WHILE…DO | REPEAT…UNTIL |
|---|---|---|---|
| Control | Count (fixed number) | Condition | Condition |
| Test position | Start (built-in) | Start | End |
| Min iterations | 0 | 0 | 1 |
| Loops while condition is… | N/A (counter in range) | TRUE | FALSE (stops when TRUE) |
| Best for | Repeating a known number of times | May not need to run at all | Must run at least once (e.g. menu, input validation) |
Summary:
- Use FOR when you know in advance how many times to repeat (e.g. process 10 students).
- Use WHILE when the loop might not need to run at all (e.g. read until EOF — file may be empty).
- Use REPEAT when the loop must run at least once (e.g. display a menu, then check if user wants to quit).
Operators, Boolean Logic & Modular Programming
Arithmetic Operators
| Operator | Operation | Example | Result |
|---|---|---|---|
| + | Addition | 7 + 4 | 11 |
| − | Subtraction | 7 − 4 | 3 |
| ∗ | Multiplication | 7 ∗ 4 | 28 |
| / | Division (real result) | 7 / 4 | 1.75 |
| DIV | Integer division (quotient only) | 7 DIV 4 | 1 |
| MOD | Modulus (remainder only) | 7 MOD 4 | 3 |
| ^ | Exponentiation (to the power of) | 2 ^ 4 | 16 |
DIV returns the integer quotient of a division (how many times the divisor goes in fully). MOD returns the remainder after that division.
For 23 ÷ 5 we have: 5 goes into 23 a total of 4 times (4 × 5 = 20), leaving a remainder of 3.
(a) 23 DIV 5 = 4
(b) 23 MOD 5 = 3
(c) 100 DIV 7 = 14 (14 × 7 = 98)
(d) 100 MOD 7 = 2 (100 − 98 = 2)
Relational & Boolean Operators
| Operator | Meaning | Example (TRUE) | Example (FALSE) |
|---|---|---|---|
| = | Equal to | 5 = 5 | 5 = 6 |
| ≠ | Not equal to | 5 ≠ 6 | 5 ≠ 5 |
| < | Less than | 3 < 7 | 7 < 3 |
| > | Greater than | 7 > 3 | 3 > 7 |
| ≤ | Less than or equal to | 5 ≤ 5 | 6 ≤ 5 |
| ≥ | Greater than or equal to | 5 ≥ 5 | 4 ≥ 5 |
Every relational expression evaluates to a BOOLEAN value — either TRUE or FALSE — which is why relational operators are often combined with Boolean operators inside IF and WHILE conditions.
Boolean operators combine or modify Boolean (TRUE/FALSE) values and produce another Boolean value.
AND — both must be TRUE
| A | B | A AND B |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | F |
| F | F | F |
OR — at least one TRUE
| A | B | A OR B |
|---|---|---|
| T | T | T |
| T | F | T |
| F | T | T |
| F | F | F |
NOT — inverts the value
| A | NOT A |
|---|---|
| T | F |
| F | T |
This uses a compound condition — two relational expressions combined with the Boolean operator AND.
Sample traces:
- Age = 20, Score = 75 → both TRUE → AND = TRUE → "Eligible".
- Age = 20, Score = 50 → second FALSE → AND = FALSE → "Not eligible".
- Age = 15, Score = 90 → first FALSE → AND = FALSE → "Not eligible".
Input/Output, Counting & Totalling
Pseudocode:
Key ideas:
- INPUT reads a value from the user (or another source) and stores it in a variable.
- OUTPUT displays one or more values on the screen, separated by commas.
- Total acts as an accumulator (initialised to 0 before the loop).
- Average is REAL because Total / Count may produce a decimal.
A counter is an INTEGER variable that increases by 1 each time a particular event happens — it counts events. An accumulator (or totalling variable) is a numeric variable that adds up a running total — it accumulates values. Both must be initialised to 0 before the loop begins.
Pseudocode:
TotalMark is the accumulator (it stores the running sum of all 20 marks). PassCount is the counter (it stores how many marks were ≥ 50).
Modular Programming: Procedures & Functions
Both procedures and functions are subroutines — named, self-contained blocks of code that perform a specific task and can be called (invoked) from anywhere in the program.
| Procedure | Function |
|---|---|
| Performs a task but does NOT return a value. | Performs a task and returns a single value to the caller. |
| Called as a statement on its own: PrintHeader() | Called inside an expression: Area ← CalcArea(5, 3) |
| Used for output, menus, input prompts. | Used for calculations and lookups. |
Why use subroutines?
- Reusability: write once, call many times.
- Readability: the main program becomes short and clear.
- Maintainability: a bug fix in the subroutine automatically applies everywhere it is called.
- Teamwork: different programmers can work on different subroutines at the same time.
(a) Procedure — no return value:
(b) Function — returns a value:
(c) Main program — calls both:
Note: Length and Width are parameters — values passed into the function when it is called. The function uses them to compute and RETURN the area, which the main program then outputs.
Revision: Statements and Key Computing Terms
| Statement | Key Term |
|---|---|
| A named storage location whose value can change during program execution. | Variable |
| A named value that does not change during program execution. | Constant |
| A data type that stores whole numbers (positive, negative or zero). | INTEGER |
| A data type that stores numbers with a fractional (decimal) part. | REAL |
| A data type that stores a single character. | CHAR |
| A data type that stores a sequence of zero or more characters. | STRING |
| A data type that can only hold the value TRUE or FALSE. | BOOLEAN |
| Executing instructions one after another, in the order they are written. | Sequence |
| Choosing between two or more paths based on a condition. | Selection |
| Repeating a block of instructions multiple times. | Iteration |
| A loop that runs a fixed, known number of times using a counter. | FOR…NEXT |
| A loop whose condition is tested at the start; may run zero times. | WHILE…DO |
| A loop whose condition is tested at the end; always runs at least once. | REPEAT…UNTIL |
| A multi-way selection structure based on the value of a single variable. | CASE…OF |
| Arithmetic operator that returns the integer quotient of a division. | DIV |
| Arithmetic operator that returns the remainder of a division. | MOD |
| Arithmetic operator that raises a number to a power. | ^ (exponent) |
| Boolean operator that returns TRUE only if both conditions are TRUE. | AND |
| Boolean operator that returns TRUE if at least one condition is TRUE. | OR |
| Boolean operator that inverts a Boolean value (TRUE ↔ FALSE). | NOT |
| A variable used to count how many times an event has occurred. | Counter |
| A variable used to keep a running total of values as a loop runs. | Accumulator (Totalling variable) |
| A statement that reads a value from the user or another source and stores it in a variable. | INPUT |
| A statement that displays one or more values on the screen. | OUTPUT |
| A self-contained block of code that performs a task but does not return a value. | Procedure |
| A self-contained block of code that performs a task and returns a single value to the caller. | Function |
| A value passed into a procedure or function when it is called. | Parameter |