Basic Algorithmic Structures
Every algorithm, no matter how complex, is built from just three fundamental structures: sequence, selection (branching), and iteration (loops). Understanding these building blocks is essential for designing correct and efficient programs.
Learning Objectives
- 11.5.3.9 Describe and apply the sequential structure
- 11.5.3.10 Describe and apply the branching structure
- 11.5.3.11 Describe and apply the loop structure
Conceptual Anchor
The Recipe Analogy
Think of an algorithm as a cooking recipe. A sequence is doing steps in order (chop, mix, cook). A branch is a decision: "If the sauce is too thick, add water." A loop is repetition: "Stir every 2 minutes until golden." Every recipe — and every algorithm — uses combinations of these three structures.
Rules & Theory
1. Sequential Structure
Statements are executed one after another, in the order they appear. There are no decisions or repetitions — just straight-line execution.
| Property | Description |
|---|---|
| Execution order | Top to bottom, left to right |
| Control flow | No branching or looping |
| Flowchart shape | Rectangles connected by straight arrows |
| Use case | Input → Process → Output |
Pseudocode:
INPUT radius
area ← 3.14159 * radius * radius
OUTPUT "Area = ", areaC++:
#include <iostream>
using namespace std;
int main() {
double radius;
cout << "Enter radius: ";
cin >> radius;
double area = 3.14159 * radius * radius;
cout << "Area = " << area << endl;
return 0;
}2. Branching Structure (Selection)
The program makes a decision based on a condition. If the condition is TRUE, one path is taken; otherwise, an alternative path is followed.
| Type | Description | Keyword |
|---|---|---|
| Simple | One branch only | IF...THEN...ENDIF |
| Two-way | Two alternatives | IF...THEN...ELSE...ENDIF |
| Multi-way | Several alternatives | CASE OF...ENDCASE |
| Nested | Condition inside condition | IF inside IF |
Pseudocode (IF/ELSE):
INPUT mark
IF mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIFPseudocode (CASE):
INPUT grade
CASE OF grade
"A": OUTPUT "Excellent"
"B": OUTPUT "Good"
"C": OUTPUT "Satisfactory"
OTHERWISE: OUTPUT "Below standard"
ENDCASEC++ (if/else):
int mark;
cin >> mark;
if (mark >= 50) {
cout << "Pass" << endl;
} else {
cout << "Fail" << endl;
}C++ (switch):
char grade;
cin >> grade;
switch (grade) {
case 'A': cout << "Excellent"; break;
case 'B': cout << "Good"; break;
case 'C': cout << "Satisfactory"; break;
default: cout << "Below standard";
}3. Loop Structure (Iteration)
A group of statements is repeated while a condition is met (or a fixed number of times). There are three main loop types:
| Loop type | Pseudocode | C++ | When to use |
|---|---|---|---|
| Count-controlled | FOR...NEXT |
for |
Known number of iterations |
| Pre-condition | WHILE...ENDWHILE |
while |
May execute 0 times |
| Post-condition | REPEAT...UNTIL |
do...while |
Always executes at least once |
FOR loop — Pseudocode:
FOR i ← 1 TO 5
OUTPUT i
NEXT i
// Output: 1 2 3 4 5FOR loop — C++:
for (int i = 1; i <= 5; i++) {
cout << i << " ";
}
// Output: 1 2 3 4 5WHILE loop — Pseudocode:
total ← 0
INPUT num
WHILE num <> -1
total ← total + num
INPUT num
ENDWHILE
OUTPUT "Total = ", totalREPEAT/UNTIL — Pseudocode:
REPEAT
OUTPUT "Enter password: "
INPUT password
UNTIL password = "secret123"
OUTPUT "Access granted"Flowchart Shapes
Oval = Start/End · Rectangle = Process · Diamond = Decision · Parallelogram = Input/Output · Arrow = Flow direction. Branching uses a diamond with two exits (Yes/No). Loops use an arrow going back to the condition diamond.
Worked Examples
1 Temperature Classifier (All Three Structures)
Read 5 temperatures and classify each as Cold, Warm, or Hot.
// Sequential: variable declaration
// Loop: FOR to repeat 5 times
// Branching: IF/ELSE to classify
FOR i ← 1 TO 5
OUTPUT "Enter temperature: "
INPUT temp
IF temp < 10 THEN
OUTPUT "Cold"
ELSE IF temp < 25 THEN
OUTPUT "Warm"
ELSE
OUTPUT "Hot"
ENDIF
NEXT i// C++ equivalent
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
double temp;
cout << "Enter temperature: ";
cin >> temp;
if (temp < 10)
cout << "Cold" << endl;
else if (temp < 25)
cout << "Warm" << endl;
else
cout << "Hot" << endl;
}
return 0;
}2 Sum Until Sentinel (WHILE loop + branching)
Keep reading numbers until the user enters 0. Output the sum and count of positive numbers.
sum ← 0
count ← 0
OUTPUT "Enter a number (0 to stop): "
INPUT num
WHILE num <> 0
IF num > 0 THEN
sum ← sum + num
count ← count + 1
ENDIF
OUTPUT "Enter a number (0 to stop): "
INPUT num
ENDWHILE
OUTPUT "Sum = ", sum, " Count = ", count3 Menu-Driven Calculator (REPEAT + CASE)
REPEAT
OUTPUT "1. Add 2. Subtract 3. Quit"
INPUT choice
CASE OF choice
1: INPUT a, b
OUTPUT "Result = ", a + b
2: INPUT a, b
OUTPUT "Result = ", a - b
3: OUTPUT "Goodbye!"
OTHERWISE: OUTPUT "Invalid choice"
ENDCASE
UNTIL choice = 3Common Pitfalls
Infinite Loops
Forgetting to update the loop variable leads to an infinite loop. Always ensure the condition will eventually become false.
Off-by-One Errors
Using < instead of <= (or vice versa) can cause your loop
to run one time too many or too few. Trace the first and last iterations carefully.
Missing ELSE / Default
Forgetting to handle the "otherwise" case in branching means unexpected inputs
produce no output. Always include ELSE or OTHERWISE.
Pro Tips
- Use FOR when you know how many times to repeat
- Use WHILE when the condition is checked before each iteration
- Use REPEAT/UNTIL when you need at least one execution (e.g., menu)
- Draw a flowchart first — it helps you see the logic clearly before coding
Tasks
Name the three basic algorithmic structures and give one real-life example of each.
Explain the difference between a pre-condition loop (WHILE) and a post-condition loop (REPEAT/UNTIL). When would you use each?
Write pseudocode that reads 10 numbers and outputs how many are even and how many are odd.
A student wrote: FOR i ← 1 TO 10 ... NEXT i but inside the loop also has
i ← i + 1. Trace the execution and explain what goes wrong.
Design a number-guessing game: the program picks a random number 1–100, the user guesses, and the program says "Higher" or "Lower" until correct. Use all three structures.
Self-Check Quiz
Q1: Which structure executes statements one after another with no decisions?
Q2: What flowchart shape represents a decision?
Q3: Which loop type always executes at least once?
Q4: What is the C++ equivalent of pseudocode CASE OF?