Programming and algorithms
Why This Matters
# Programming and Algorithms Summary This fundamental Computer Science topic covers algorithm design, pseudocode, flowcharts, and programming constructs including variables, data types, loops, conditionals, and arrays. Students learn to trace algorithms, identify computational thinking strategies (decomposition, pattern recognition, abstraction), and evaluate algorithm efficiency. Essential for Paper 1 exam questions on code analysis and algorithm design, this unit develops problem-solving skills transferable to practical programming tasks in Papers 2 and 3, forming the foundation for higher-level topics like recursion and object-oriented programming.
Key Words to Know
Core Concepts & Theory
Programming is the process of designing, writing, testing, and maintaining source code to create computer programs that solve specific problems. Algorithms are step-by-step procedures or formulas for solving problems—they form the logical foundation of all programs.
Key Algorithmic Concepts:
Sequence refers to instructions executed in order, one after another. Selection (conditional statements) allows different paths based on conditions using IF-THEN-ELSE structures. Iteration (loops) repeats instructions—either a fixed number of times (count-controlled with FOR loops) or until a condition is met (condition-controlled with WHILE/REPEAT-UNTIL loops).
Variables are named storage locations holding data values that can change during program execution. Constants hold fixed values that never change. Data types include INTEGER (whole numbers), REAL/FLOAT (decimal numbers), STRING (text), BOOLEAN (TRUE/FALSE), and CHAR (single character).
Operators perform operations: arithmetic (+, -, , /, MOD, DIV), comparison (=, ≠, <, >, ≤, ≥), and logical (AND, OR, NOT).
Pseudocode is a structured, English-like notation for describing algorithms without strict syntax rules. Cambridge uses specific conventions: DECLARE for variables, INPUT/OUTPUT for I/O, IF-THEN-ELSE-ENDIF for selection, and FOR/WHILE/REPEAT-UNTIL loops.
Memory Aid (SISAL): Sequence, Iteration, Selection form ALgorithms—the three fundamental programming constructs that can solve any computable problem (this is the structured programming theorem).
Essential Formula: Algorithm Efficiency = f(n) where n = input size. Linear algorithms O(n) scale proportionally; quadratic O(n²) algorithms slow dramatically with larger inputs.
Detailed Explanation with Real-World Examples
Think of an algorithm like a recipe: precise, ordered instructions anyone can follow to achieve the same result. Just as recipes have ingredients (variables/constants), preparation steps (sequence), conditional instructions ("if mixture is too thick, add milk"), and repetitive actions ("stir until smooth"), programs mirror this structure.
Real-World Algorithm: Traffic Light System
Traffic lights demonstrate all three programming constructs. The sequence cycles through states (red→amber→green). Selection occurs when sensors detect vehicles ("IF car waiting AND cross-street clear THEN change light"). Iteration continuously loops through the cycle until the system powers down.
Netflix Recommendation Algorithm
When you browse Netflix, complex algorithms analyze your viewing history (stored in variables). The system uses selection to categorize you: "IF user watches >5 sci-fi shows THEN increase sci-fi recommendations." It employs iteration to examine thousands of titles, calculating match percentages. Arrays store your watch history, while Boolean flags track "liked/disliked."
E-commerce Shopping Cart
Adding items uses sequence (select product → add to cart → update total). Variables track quantity and price. Selection applies discounts: "IF total > $50 THEN apply 10% discount." Iteration processes each cart item to calculate the final bill. The MOD operator might determine: "IF itemCount MOD 2 = 0 THEN free_shipping = TRUE" (free shipping on even quantities).
GPS Navigation
Your GPS uses Dijkstra's algorithm—a sophisticated example of iteration and selection working together. It repeatedly (iteration) examines possible routes, selecting (selection) the shortest path based on conditions like traffic, distance, and road type. Variables store current location, destination, and estimated time.
Worked Examples & Step-by-Step Solutions
Example 1: Temperature Converter with Validation
Question: Write an algorithm that converts Celsius to Fahrenheit for 5 temperatures, rejecting invalid inputs below absolute zero (-273.15°C).
DECLARE Celsius, Fahrenheit : REAL
DECLARE Count : INTEGER
FOR Count ← 1 TO 5
REPEAT
OUTPUT "Enter temperature in Celsius: "
INPUT Celsius
IF Celsius < -273.15
THEN
OUTPUT "Error: Below absolute zero!"
ENDIF
UNTIL Celsius >= -273.15
Fahrenheit ← (Celsius * 9/5) + 32
OUTPUT Celsius, " °C = ", Fahrenheit, " °F"
NEXT Count
DECLARE Celsius, Fahrenheit : REAL
DECLARE Count : INTEGER
FOR Count ← 1 TO 5
REPEAT
OUTPUT "Enter temperature in Celsius: "
INPUT Celsius
IF Celsius < -273.15
THEN
OUTPUT "Error: Below absolute zero!"
ENDIF
UNTIL Celsius >= -273.15
Fahrenheit ← (Celsius * 9/5) + 32
OUTPUT Celsius, " °C = ", Fahrenheit, " °F"
NEXT Count
Examiner Note: This demonstrates nested iteration (REPEAT inside FOR) and validation through selection—worth 2+ marks in Cambridge papers.
Example 2: Array Search with Flag
Question: Search an array of 10 student names for "Ahmed". Output position if found, otherwise "Not found".
DECLARE Names : ARRAY[1:10] OF STRING
DECLARE Found : BOOLEAN
DECLARE Position, Index : INTEGER
Found ← FALSE
Index ← 1
WHILE Index <= 10 AND Found = FALSE
IF Names[Index] = "Ahmed"
THEN
Found ← TRUE
Position ← Index
ENDIF
Index ← Index + 1
ENDWHILE
IF Found = TRUE
THEN
OUTPUT "Found at position ", Position
ELSE
OUTPUT "Not found"
ENDIF
DECLARE Names : ARRAY[1:10] OF STRING
DECLARE Found : BOOLEAN
DECLARE Position, Index : INTEGER
Found ← FALSE
Index ← 1
WHILE Index <= 10 AND Found = FALSE
IF Names[Index] = "Ahmed"
THEN
Found ← TRUE
Position ← Index
ENDIF
Index ← Index + 1
ENDWHILE
IF Found = TRUE
THEN
OUTPUT "Found at position ", Position
ELSE
OUTPUT "Not found"
ENDIF
Examiner Note: The Boolean flag prevents unnecessary searching after finding the target—demonstrates efficiency (Cambridge mark schemes reward this). The compound condition "AND Found = FALSE" is crucial.
Example 3: Total & Average Calculator
Question: Calculate total and average of numbers until -1 entered.
DECLARE Number, Total : REAL
DECLARE Count : INTEGER
Total ← 0
Count ← 0
INPUT Number
WHILE Number <> -1
Total ← Total + Number
Count ← Count + 1
INPUT Number
ENDWHILE
IF Count > 0
THEN
OUTPUT "Average: ", Total / Count
ELSE
OUTPUT "No numbers entered"
ENDIF
DECLARE Number, Total : REAL
DECLARE Count : INTEGER
Total ← 0
Count ← 0
INPUT Number
WHILE Number <> -1
Total ← Total + Number
Count ← Count + 1
INPUT Number
ENDWHILE
IF Count > 0
THEN
OUTPUT "Average: ", Total / Count
ELSE
OUTPUT "No numbers entered"
ENDIF
Examiner Note: Division-by-zero check (IF Count > 0) essential for full marks.
Common Exam Mistakes & How to Avoid Them
Mistake 1: Incorrect Loop Boundaries (Off-by-One Errors)
Why it happens: Students confuse "<" with "<=" or use wr...
Cambridge Exam Technique & Mark Scheme Tips
Understanding Command Words (Critical for IB):
"State/Identify" (1 mark): Brief answer, no explanation. Example...
2 more sections locked
Upgrade to Starter to unlock all study notes, audio listening, and more.
Exam Tips
- 1.When asked to describe an algorithm, always use clear, numbered steps or a flowchart/pseudocode; don't just write a paragraph.
- 2.Practice tracing algorithms: mentally follow the steps of an algorithm with specific input values to predict the output.
- 3.Understand the difference between an algorithm (the plan) and a program (the coded instructions) – this is a common distinction in questions.
- 4.Be able to explain the purpose of different types of algorithms (e.g., sorting, searching) and give simple examples.
- 5.For debugging questions, identify the error, explain why it's an error, and propose a correct solution.