Write algorithms with decision-making (branching, looping)

4 Algorithms and Flowcharts

What is an Algorithm?

An algorithm is a step‑by‑step recipe that solves a problem or performs a task. Think of it like a cooking recipe: you follow the instructions in order to get a tasty dish. In computing, the recipe is written in plain language or a diagram.

Key properties:

  • Well‑defined steps
  • Finite number of steps
  • Input and output
  • Deterministic (same input gives same output)

Flowcharts – Visualising Algorithms

Flowcharts use shapes to represent actions:

  • Oval – Start/End
  • Rectangle – Process/Instruction
  • Diamond – Decision (branch)
  • Parallelogram – Input/Output

Example: Check if a number is even or odd

  1. Start
  2. Input number $n$
  3. Decision: Is $n \bmod 2 = 0$?
  4. If yes → Output “Even”
  5. If no → Output “Odd”
  6. End

🧩 Flowcharts help you spot errors before coding.

Decision-Making (Branching)

Branching lets the algorithm choose a path based on a condition.

Syntax in pseudocode:

IF condition THEN
    // do something
ELSE
    // do something else
END IF

Example: Grade a test

  1. Input score $s$
  2. IF $s \ge 90$ THEN Grade = A
  3. ELSE IF $s \ge 80$ THEN Grade = B
  4. ELSE IF $s \ge 70$ THEN Grade = C
  5. ELSE Grade = F

🔀 Remember: each IF can have an ELSE IF or ELSE.

Looping (Repetition)

Loops repeat a block of code while a condition holds.

Common loop types:

  • FOR – fixed number of iterations
  • WHILE – until condition is false
  • DO‑WHILE – execute at least once

Example: Sum the first 10 natural numbers

sum = 0
FOR i = 1 TO 10
    sum = sum + i
END FOR
// sum = 55

🔄 Loops are like a conveyor belt that keeps moving items until a stop signal.

Putting It All Together – A Mini‑Project

Write an algorithm to find the largest of three numbers.

  1. Start
  2. Input $a$, $b$, $c$
  3. Set $max = a$
  4. IF $b > max$ THEN $max = b$
  5. IF $c > max$ THEN $max = c$
  6. Output $max$
  7. End

🛠️ Test with different inputs to ensure it works.

Exam Tips

Tip Why It Helps
Use clear labels (Start, End, Decision) Reduces confusion for examiners.
Show all steps in loops (initialisation, condition, update) Demonstrates understanding of loop mechanics.
Use pseudocode before drawing flowchart Helps organise thoughts and catch errors early.
Check edge cases (e.g., zero, negative numbers) Shows thoroughness and robustness.

📚 Practice by writing algorithms for everyday tasks – like making a sandwich or planning a school trip. The more you practice, the easier it becomes!

Revision

Log in to practice.

1 views 0 suggestions