Master nested conditions, nested loops, 2D arrays, integrated control structures, and algorithm comparison to build powerful, efficient programs.
Placing a control structure inside another is called a nested structure. A nested conditional statement decides whether to enter another conditional statement based on the result of the previous condition.
Pseudocode / Python
Nested equivalent
A conditional statement can be nested in not only the 'else' branch but also other branches, allowing complex decision trees.
Given three side lengths a, b, c — classify the triangle type:
Placing a loop inside another loop is called a nested loop. Each time the outer loop body executes once, the inner loop body executes completely.
Pseudocode
Python
For the example above (i: 0→1, j: 0→2), the trace table is:
| Line | i | j | Output | Note |
|---|---|---|---|---|
| for i←0 | 0 | |||
| Output i | 0 | |||
| for j←0 | 0 | |||
| Output j | 0 | Inner (i=0) | ||
| for j←1 | 1 | |||
| Output j | 1 | |||
| for j←2 | 2 | |||
| Output j | 2 | |||
| for i←1 | 1 | |||
| Output i | 1 | |||
| for j←0 | 0 | |||
| Output j | 0 | Inner (i=1) | ||
| for j←1 | 1 | |||
| Output j | 1 | |||
| for j←2 | 2 | |||
| Output j | 2 |
Design an algorithm to draw a square of * with side length L.
We often use a nested for loop to access items in a two-dimensional array one by one. The outer loop iterates over rows (i), and the inner loop iterates over columns (j).
Row-first (common)
Column-first (alternative)
Before accessing items in a 2D list, you must first create it and assign default values. This process is called initialisation.
Jonathan's bookshelf is a 3×6 2D array B. B[i,j] stores the number of books. Calculate the total.
| j=0 | j=1 | j=2 | j=3 | j=4 | j=5 | |
|---|---|---|---|---|---|---|
| i=0 | 5 | 9 | 6 | 5 | 3 | 4 |
| i=1 | 2 | 3 | 8 | 2 | 6 | 5 |
| i=2 | 8 | 3 | 5 | 3 | 9 | 9 |
Photos are lattices of pixels. Each pixel's colour can be represented as an integer. Using 2D arrays and nested loops, we can perform image operations like:
Control structures (sequence, selection, iteration) can be combined with data structures (1D/2D arrays) to form multi-layered nested structures. This allows us to design algorithms that solve complex real-world problems.
A 5×5 Bingo grid is stored in 2D array result[i,j]. True = drawn, False = not drawn. Check if any row is fully crossed.
Search for a word horizontally in a 10×10 letter grid:
Copy a 3×3 2D array ttt_2d to a 1D array ttt_1d row by row:
A problem can often be solved using several algorithms. We compare algorithms to select the most correct and appropriate solution. Algorithms can be compared in two main aspects: steps of operation and resource usage.
More steps of operation → longer running time → higher time complexity. Fewer steps = more efficient.
More computer resources (memory, variables) used → higher space complexity. Different data structures affect space usage.
| Criterion | Description | Key Question |
|---|---|---|
| Correctness | Does the algorithm produce the right answer for all valid inputs? | Are there edge cases it fails? |
| Appropriateness | Is the algorithm suitable for the given problem constraints? | Does it handle all required cases? |
| Steps of Operation | How many operations does the algorithm perform? | Which algorithm is faster? |
| Resource Usage | How much memory/variables does the algorithm use? | Which algorithm uses less memory? |
| Data Structures | What data structures does the algorithm use? | Are they appropriate for the problem? |
A box should contain items A to A+9. One item is missing. Array C stores the actual items. Find the missing item number.
Answer all 12 questions then click Submit to see your score.
Click any card to reveal the definition.
Filter by difficulty and click 'View Reference Answer' to see model answers.
(a) Total = 96 (thousands)
(b) population←0; for i from 1 to 5; for j from 1 to 7; population←population+map[i,j]; Output population
(a) 5 times. (b) 6 times per outer iteration. (c) 5 × 6 = 30 total.
(a) fail←0; for i from 0 to 3; for j from 0 to 9; if form5_chi[i][j] < 50: fail+=1; print(fail)
(b) highest_mark = form5_chi[0][0]; for i in range(4): for j in range(10): if form5_chi[i][j] > highest_mark: highest_mark = form5_chi[i][j]
for i from 1 to 10; for j from 1 to 10; M[i,j] ← i * j
match←True; for p from 1 to 3; for q from 1 to 3; if human[p,q] ≠ photo[i+p-1, j+q-1] then match←False; Output match. Blanks: True, p, q, i+p-1, j+q-1, False.
(i) Algorithm 3 uses ~30 steps vs Algorithm 2's ~100 steps. Algorithm 3 is more time-efficient.
(ii) Algorithm 1 uses only 2 variables vs Algorithm 3's boolean array. Algorithm 1 has lower space complexity.
(iii) Algorithm 1 is more efficient (~20 steps vs ~100 steps).