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.
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
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.
- Start (Oval)
- INPUT N (Parallelogram)
- sum ← 0 (Rectangle)
- i ← 1 (Rectangle)
- i ≤ N? (Diamond)
- If No, go to step 8.
- If Yes, continue to step 5.
- sum ← sum + i (Rectangle)
- i ← i + 1 (Rectangle)
- Back to step 4.
- WRITE sum (Parallelogram)
- 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.
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:
INPUT value– Parallelogramsum ← sum + i– Rectanglei ≤ N?– DiamondWRITE 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.