A variable stores a value that can be used or changed, a conditional selects an action depending on whether a condition is true or false, and a loop repeats instructions. These are distinct components of algorithms, but they often work together.
This topic is part of IB Digital Society 3.2 Algorithms and is shared by SL and HL students. An algorithm is a finite sequence of unambiguous instructions designed to solve a problem or perform a task.
| Component | Function | Simple pseudocode example |
|---|---|---|
| Variable | Stores data under a name so that the algorithm can access or update it | score = 0 |
| Conditional | Uses a condition to determine which instruction runs | IF score >= 50 THEN display "Pass" |
| Loop | Repeats instructions while a condition applies or for a specified number of times | FOR question = 1 TO 10 |
Consider an algorithm that marks a ten-question quiz. The variable score begins at zero. A loop processes each answer, and a conditional checks whether that answer is correct. If it is correct, the algorithm updates the variable using score = score + 1.
Step by step, the loop controls repetition, the conditional controls selection, and the variable preserves the changing score. Without the loop, the checking instructions would need to be written ten times. Without the conditional, the algorithm could not respond differently to correct and incorrect answers. Without the variable, it could not retain the accumulated score.
A common misconception is that a conditional automatically repeats instructions. It does not. A conditional makes a decision once whenever execution reaches it; a loop causes repetition, although it may use a condition to decide when to stop.
In an IB exam, the command term distinguish requires you to make the differences clear. State each component's function, then use one algorithm to show how storage, selection and repetition operate differently.