24 min read
Pseudocode for Selection and Loops
IF…THEN…ELSE, nested IFs, WHILE, FOR and REPEAT…UNTIL — each shown next to its flowchart.
IF … THEN … ENDIF
One-way selection: the shop discountPseudocode
BEGIN INPUT total IF total > 5000 THEN total ← total × 0.9 ENDIF OUTPUT totalENDThe same algorithm as a flowchart
Read this flowchart as text
- Terminator: Start (then → INPUT total)
- Input/output: INPUT total (then → total > 5000?)
- Decision: total > 5000? (Yes → total = total × 0.9; No → OUTPUT total)
- Process: total = total × 0.9 (then → OUTPUT total)
- Input/output: OUTPUT total (then → Stop)
- Terminator: Stop
IF … THEN … ELSE … ENDIF
Two-way selectionPseudocode
BEGIN INPUT mark IF mark >= 40 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIFENDNested IF
Cinema ticket pricePseudocode
BEGIN INPUT age IF age < 12 THEN price ← 600 ELSE IF age >= 60 THEN price ← 800 ELSE price ← 1200 ENDIF ENDIF OUTPUT priceENDThe nested decisions as a flowchart
Read this flowchart as text
- Terminator: Start (then → INPUT age)
- Input/output: INPUT age (then → age < 12?)
- Decision: age < 12? (Yes → price = 600; No → age >= 60?)
- Process: price = 600 (then → OUTPUT price)
- Decision: age >= 60? (Yes → price = 800; No → price = 1200)
- Process: price = 800 (then → OUTPUT price)
- Process: price = 1200 (then → OUTPUT price)
- Input/output: OUTPUT price (then → Stop)
- Terminator: Stop
WHILE … ENDWHILE
Add prices until 0 is enteredPseudocode
BEGIN total ← 0 INPUT price WHILE price > 0 total ← total + price INPUT price ENDWHILE OUTPUT totalENDThe WHILE loop as a flowchart
Read this flowchart as text
- Terminator: Start (then → total = 0)
- Process: total = 0 (then → INPUT price)
- Input/output: INPUT price (then → price > 0?)
- Decision: price > 0? (Yes → total = total + price; No → OUTPUT total)
- Process: total = total + price (then → INPUT price)
- Input/output: OUTPUT total (then → Stop)
- Input/output: INPUT price (then → price > 0?)
- Terminator: Stop
A WHILE loop checks the condition before each repeat. If the very first price is 0, the loop body never runs and the output is 0.
FOR … TO … NEXT
Output the odd numbers 1 to 9Pseudocode
BEGIN FOR number ← 1 TO 9 STEP 2 OUTPUT number NEXT numberENDA FOR loop drawn as a flowchart
Read this flowchart as text
- Terminator: Start (then → number = 1)
- Process: number = 1 (then → number <= 9?)
- Decision: number <= 9? (Yes → OUTPUT number; No → Stop)
- Input/output: OUTPUT number (then → number = number + 2)
- Terminator: Stop
- Process: number = number + 2 (then → number <= 9?)
A FOR loop is a count-controlled loop: the counter starts, is checked and is increased automatically. STEP 2 means "add 2 each time"; without STEP the counter goes up by 1.
REPEAT … UNTIL
Ask until the correct PIN is enteredPseudocode
BEGIN REPEAT INPUT pin UNTIL pin = 4321 OUTPUT "Welcome"ENDREPEAT checks after each repeat, so the body always runs at least once — right for asking for a PIN.
WHILE vs REPEAT … UNTIL
| WHILE | REPEAT … UNTIL |
|---|---|
| Checks the condition first | Checks the condition last |
| May run zero times | Always runs at least once |
| Repeats while the condition is true | Repeats until the condition becomes true |
Practice
More lessons in Algorithms, Flowcharts and Pseudocode · Next: Trace Tables · Previous: Pseudocode Basics
