Every algorithm, however large, is assembled from a handful of control structures. This page explains the three basic ones and two structuring tools that make big algorithms manageable.
The three basic control structures
These three are enough to express any algorithm. This result is known as the Böhm–Jacopini theorem (1966), the basis of structured programming.
In Euclid’s algorithm:
Lines
Building block
Why
r ← a mod b; a ← b; b ← r
Sequencing
The order matters: swapping the last two lines gives a wrong answer.
while b ≠ 0 do
Iteration
The three lines repeat while the condition is true.
The test b ≠ 0
Condition of the loop
Each time it is checked, it decides between “repeat the body” and “leave the loop”.
The study material describes the while line as both selection and iteration. It is better to call it iteration: a loop contains a test, but its purpose is repetition. A pure selection is an if statement.
Procedures
When the same group of steps is needed in many places, we write it once as a procedure (also called a subprogram, function or method) and call it with different arguments. A procedure that returns a value uses a return statement.
C · sum_procedure.c
#include <stdio.h>/* A procedure (function) written once and called three times. */int sum_of_array(const int a[], int n){ int total = 0; for (int i = 0; i < n; i++) /* iteration */ total += a[i]; /* sequencing inside the loop */ return total;}int main(void){ int marks[] = {72, 65, 88}; int prices[] = {120, 80, 45, 55}; int empty[1] = {0}; printf("Total marks : %d\n", sum_of_array(marks, 3)); printf("Total price : %d\n", sum_of_array(prices, 4)); printf("Empty list : %d\n", sum_of_array(empty, 0)); return 0;}
Output
Total marks : 225Total price : 300Empty list : 0
Recursion
The classic example is the factorial: n!=n×(n−1)! for n≥1, and 0!=1.
A recursive procedure is correct only if:
it has at least one base case that is solved without a further call;
every recursive call uses a smaller argument that moves towards a base case;
the system can keep track of unfinished calls. Every modern language does this with the call stack: each call gets its own stack frame holding its parameters, local variables and return address.
C · factorial_trace.c
#include <stdio.h>/* Recursive factorial that prints each call and each return. depth is used only to indent the trace. */unsigned long long factorial(unsigned int n, int depth){ printf("%*scall factorial(%u)\n", depth * 2, "", n); if (n <= 1) { /* base case */ printf("%*sreturn 1\n", depth * 2, ""); return 1; } unsigned long long result = n * factorial(n - 1, depth + 1); /* recursive case */ printf("%*sreturn %llu\n", depth * 2, "", result); return result;}int main(void){ unsigned long long f = factorial(4, 0); printf("4! = %llu\n", f); return 0;}
The base case is n <= 1, which also handles 0!=1. The study material’s version uses only n equals 1 as the base case, so a call with n=0 would never stop.
Sequencing steps in written order
Selection if–then–else decision
Iteration loop: repeat while true
Procedure named block, called many times
Recursion procedure calls itself on a smaller case
The first three are sufficient for any algorithm; the last two make large algorithms easier to write.
The five building blocks
Key takeaways
Sequencing, selection and iteration are sufficient for any algorithm (Böhm–Jacopini).
A procedure is written once and called many times with different arguments.
A recursive procedure needs a base case and must move towards it on every call.
The call stack stores one frame per active call; deep recursion uses more memory.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
State the conditions a recursive procedure must satisfy and show them in a recursive algorithm for the sum 1+2+⋯+n.
Show worked answer
(1) A base case solved without recursion; (2) each call uses a smaller argument that moves towards the base case; (3) the system supports calls through a call stack. Algorithm: Sum(n): if n = 0 then return 0 (base case) else return n + Sum(n − 1) (smaller argument). Example: Sum(3) = 3 + Sum(2) = 3 + 2 + Sum(1) = 3 + 2 + 1 + Sum(0) = 6.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-211/basics-of-an-algorithm-and-its-properties/building-blocks-of-algorithms/ to read this page.