Use pseudocode (INPUT, WRITE, FOR, WHILE)

4 Algorithms and Flowcharts

Algorithms are step‑by‑step instructions that solve a problem. Flowcharts are visual diagrams that show the flow of these steps. In this lesson we’ll learn how to write algorithms in pseudocode using the keywords INPUT, WRITE, FOR, and WHILE, and how to translate them into flowcharts.

Pseudocode Basics

Think of pseudocode like a recipe. It tells the computer (or a human) what to do, but it’s written in plain English so it’s easy to read.

Exam Tip: When writing pseudocode for the exam, keep it clear and concise. Use the exact keywords the specification asks for (INPUT, WRITE, FOR, WHILE). Avoid unnecessary words.

Key Keywords

  • INPUT – Ask the user for data.
  • WRITE – Display information to the user.
  • FOR – Repeat a block a known number of times.
  • WHILE – Repeat a block while a condition is true.

Example: Sum of the First N Integers

Let’s write pseudocode to calculate the sum of the first N positive integers.

Step Pseudocode
1 INPUT N
2 sum ← 0
3 FOR i FROM 1 TO N DO
4 sum ← sum + i
5 END FOR
6 WRITE sum

Flowchart Conversion

Flowcharts use shapes to represent different operations:

  • Oval – Start/End
  • Parallelogram – INPUT/WRITE
  • Rectangle – Assignment or calculation
  • Diamond – Decision (IF/WHILE)
  • Arrow – Flow direction
Exam Tip: When drawing a flowchart, always start with an Oval labelled “Start” and finish with an Oval labelled “End”. Use clear labels inside shapes and keep arrows pointing downwards or to the right for readability.

Flowchart Example: Sum of the First N Integers

Below is a textual representation of the flowchart for the same algorithm. Visualise each shape as described.

  1. Start (Oval)
  2. INPUT N (Parallelogram)
  3. sum ← 0 (Rectangle)
  4. i ← 1 (Rectangle)
  5. i ≤ N? (Diamond)
    • If No, go to step 8.
    • If Yes, continue to step 5.
  6. sum ← sum + i (Rectangle)
  7. i ← i + 1 (Rectangle)
  8. Back to step 4.
  9. WRITE sum (Parallelogram)
  10. End (Oval)

Analogy: Cooking a Meal

Imagine you’re cooking a simple dish:

  • INPUT – You ask the kitchen assistant for the ingredients.
  • WRITE – You announce the dish is ready.
  • FOR – You repeat chopping the same vegetable 5 times.
  • WHILE – You keep stirring until the sauce thickens.

Just like a recipe, an algorithm tells the computer exactly what to do, step by step.

Exam Practice Question

Write pseudocode to find the largest number in a list of n numbers entered by the user. Use INPUT, WRITE, FOR, and WHILE where appropriate.

Exam Tip: Start by reading all numbers into an array, then use a FOR loop to compare each element with the current maximum. Remember to initialise the maximum with the first element to avoid errors.

Quick Check: Pseudocode vs Flowchart

Match the pseudocode snippet to the correct flowchart shape:

  1. INPUT value – Parallelogram
  2. sum ← sum + i – Rectangle
  3. i ≤ N? – Diamond
  4. WRITE sum – Parallelogram

Final Thought

Algorithms are the backbone of all software. Mastering pseudocode and flowcharts gives you a solid foundation for writing clear, efficient programs. Keep practising, and soon you’ll be able to design complex solutions with confidence! 🚀

Revision

Log in to practice.

0 views 0 suggestions