8.1 Programming Concepts

Question Bank · 26 Questions · 3 Parts (8.1a · 8.1b · 8.1c)

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.
PART 8.1a

Variables, Constants & Data Types

8 Questions · Q1–Q8

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):

DECLARE Age : INTEGER Age ← 15 OUTPUT "Your age is ", Age

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):

CONSTANT Pi = 3.14159 Radius ← 5.0 Area ← Pi ∗ Radius ^ 2 OUTPUT Area
Variable Constant
Value can change during program executionValue remains fixed throughout the program
Declared with DECLARE and assigned with Declared with CONSTANT keyword
Used for data input by the user, counters, totalsUsed for fixed values like Pi, tax rates, conversion factors
Example: Age ← 15Example: 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.

⚠ Note: Other examples include NumberOfDaysInWeek = 7, HoursPerDay = 24, Gravity = 9.81, MAX_SCORE = 100.

Data Types

Data Type Description Example
INTEGERA whole number (positive, negative or zero)42, −7, 0
REALA number with a fractional (decimal) part3.14, −0.5, 9.81
CHARA single character (letter, digit or symbol)'A', '7', '?'
STRINGA sequence of zero or more characters"Hello", "CS0478"
BOOLEANA logical value: either TRUE or FALSETRUE, 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:

DECLARE Age : INTEGER DECLARE Price : REAL DECLARE Initial : CHAR DECLARE FullName : STRING DECLARE IsMember : BOOLEAN CONSTANT FreeShip = 50.0 Age ← 16 Price ← 12.99 Initial ← 'S' FullName ← "Shyam Subrahmanya" IsMember ← TRUE OUTPUT "Age : ", Age OUTPUT "Price : $", Price OUTPUT "Initial : ", Initial OUTPUT "Full name : ", FullName OUTPUT "Member? : ", IsMember OUTPUT "Free ship : $", FreeShip

Output:

Age : 16 Price : $12.99 Initial : S Full name : Shyam Subrahmanya Member? : TRUE Free ship : $50.0
PART 8.1b

Sequence, Selection & Iteration

9 Questions · Q9–Q17

The Three Programming Constructs

Construct Definition Example
SequenceInstructions are executed one after another, in the order they are written.INPUT X
Y ← X + 5
OUTPUT Y
SelectionA condition is tested; the program chooses which path of instructions to execute.IF X > 10
  THEN OUTPUT "Big"
  ELSE OUTPUT "Small"
IterationA 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:

INPUT Mark IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF

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:

INPUT Mark IF Mark >= 90 THEN OUTPUT "Grade A" ELSE IF Mark >= 75 THEN OUTPUT "Grade B" ELSE IF Mark >= 50 THEN OUTPUT "Grade C" ELSE OUTPUT "Grade F" ENDIF ENDIF ENDIF ENDIF

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:

INPUT DayNum CASE OF DayNum 1 : OUTPUT "Monday" 2 : OUTPUT "Tuesday" 3 : OUTPUT "Wednesday" 4 : OUTPUT "Thursday" 5 : OUTPUT "Friday" 6 : OUTPUT "Saturday" 7 : OUTPUT "Sunday" OTHERWISE OUTPUT "Invalid day number" ENDCASE

The OTHERWISE clause runs if none of the listed values match — useful for handling invalid input.

Feature IF…THEN…ELSE CASE…OF
TestsA Boolean condition (e.g. X > 50)Discrete values of one variable (e.g. 1, 2, 3)
Range testingNaturally supports ranges (e.g. Mark >= 75)Each value must be listed separately — ranges are awkward
ReadabilityBecomes clumsy with many conditions (deeply nested)Very clean for many discrete values
Compound conditionsSupports AND / OR / NOT in conditionsTests only one variable at a time
Default pathELSE clauseOTHERWISE 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:

FOR N ← 1 TO 5 OUTPUT N ∗ N NEXT N

Output:

1 4 9 16 25

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:

Total ← 0 INPUT Num WHILE Num ≠ 0 DO Total ← Total + Num INPUT Num ENDWHILE OUTPUT "Total = ", Total

Sample trace (input: 5, 3, 7, 0):

Num=5 -> Total = 5 Num=3 -> Total = 8 Num=7 -> Total = 15 Num=0 -> loop ends Output: Total = 15

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:

REPEAT INPUT Password UNTIL LENGTH(Password) >= 6 OUTPUT "Password accepted."

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
ControlCount (fixed number)ConditionCondition
Test positionStart (built-in)StartEnd
Min iterations001
Loops while condition is…N/A (counter in range)TRUEFALSE (stops when TRUE)
Best forRepeating a known number of timesMay not need to run at allMust 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).
PART 8.1c

Operators, Boolean Logic & Modular Programming

9 Questions · Q18–Q26

Arithmetic Operators

Operator Operation Example Result
+Addition7 + 411
Subtraction7 − 43
Multiplication7 ∗ 428
/Division (real result)7 / 41.75
DIVInteger division (quotient only)7 DIV 41
MODModulus (remainder only)7 MOD 43
^Exponentiation (to the power of)2 ^ 416
⚠ Order of operations (BIDMAS): Brackets → Indices (^) → Division/Multiplication (/ ∗ DIV MOD) → Addition/Subtraction (+ −). Operators of equal precedence are evaluated left to right.

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)

⚠ Common use of MOD: N MOD 2 = 0 tests whether N is even. N MOD 10 gives the last digit of N.

Relational & Boolean Operators

Operator Meaning Example (TRUE) Example (FALSE)
=Equal to5 = 55 = 6
Not equal to5 ≠ 65 ≠ 5
<Less than3 < 77 < 3
>Greater than7 > 33 > 7
Less than or equal to5 ≤ 56 ≤ 5
Greater than or equal to5 ≥ 54 ≥ 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

ABA AND B
TTT
TFF
FTF
FFF

OR — at least one TRUE

ABA OR B
TTT
TFT
FTT
FFF

NOT — inverts the value

ANOT A
TF
FT
⚠ Order of precedence: NOT is evaluated first, then AND, then OR. Use brackets to make the order explicit, e.g. (A OR B) AND C.

This uses a compound condition — two relational expressions combined with the Boolean operator AND.

INPUT Age INPUT Score IF (Age >= 18) AND (Score >= 60) THEN OUTPUT "Eligible" ELSE OUTPUT "Not eligible" ENDIF

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".
Tip: if the rule had been "Age 18+ OR Score 60+", we would use OR instead of AND.

Input/Output, Counting & Totalling

Pseudocode:

CONSTANT Count = 5 DECLARE Total : INTEGER DECLARE Average : REAL DECLARE Num : INTEGER DECLARE I : INTEGER Total ← 0 FOR I ← 1 TO Count OUTPUT "Enter number ", I, " : " INPUT Num Total ← Total + Num NEXT I Average ← Total / Count OUTPUT "Total = ", Total OUTPUT "Average = ", Average

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:

CONSTANT NumStudents = 20 DECLARE Mark : INTEGER DECLARE TotalMark : INTEGER DECLARE PassCount : INTEGER DECLARE I : INTEGER DECLARE Average : REAL TotalMark ← 0 PassCount ← 0 FOR I ← 1 TO NumStudents INPUT Mark TotalMark ← TotalMark + Mark // accumulator IF Mark >= 50 THEN PassCount ← PassCount + 1 // counter ENDIF NEXT I Average ← TotalMark / NumStudents OUTPUT "Number of passes = ", PassCount OUTPUT "Average mark = ", Average

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:

PROCEDURE PrintWelcome OUTPUT "************************" OUTPUT "* Welcome to IGCSE CS *" OUTPUT "************************" ENDPROCEDURE

(b) Function — returns a value:

FUNCTION CalcArea(Length : INTEGER, Width : INTEGER) RETURNS INTEGER DECLARE Area : INTEGER Area ← Length ∗ Width RETURN Area ENDFUNCTION

(c) Main program — calls both:

CALL PrintWelcome DECLARE L : INTEGER DECLARE W : INTEGER INPUT L INPUT W OUTPUT "Area of rectangle = ", CalcArea(L, W)

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