GoalWrite and follow an IF ... THEN ... ELSE statement and recognise valid pseudocode written in different styles.
New words
ELSE · the part of an IF statement that runs when the condition is falsebranch · one of the two paths a selection can followstyle · the way something is written, such as capital letters or END IF in two words
Explanation
An IF ... THEN ... ELSE statement has two branches. THEN is followed by the lines for a true condition, and ELSE by the lines for a false one. Exactly one branch runs every time, never both and never neither. ENDIF closes the whole statement. Indent both branches so the structure is easy to see. Because pseudocode has no fixed rules, the same statement can be written in different valid styles, for example IF in capitals or lower case, or END IF in two words. The rule is to be clear and to keep to one style all the way through an algorithm.
Worked examples
Pseudocode
1INPUT days
2IF days > 0 THEN
3 cost ← days * 24 OUTPUT "Fine: ", cost
5ELSE
6 OUTPUT "Returned on time"7ENDIF
A library fine program. The fine is 2 for each day late. With 3 days late the output is Fine: 6. With 0 days it is Returned on time. Both branches can hold more than one line.
Pseudocode
1INPUT age
2IF age >= 60 THEN
3 price ← 44ELSE
5 price ← 76ENDIF
7OUTPUT price
Here is the ticket flowchart written as pseudocode. Both branches assign to price, then OUTPUT price runs after ENDIF. For age 60 it shows 4, and for age 59 it shows 7.
Style
Statement
1
IF seats >= 30 THEN / OUTPUT "Coach" / ELSE / OUTPUT "Minibus" / ENDIF
2
if seats >= 30 then / output "Coach" / else / output "Minibus" / endif
3
IF seats >= 30 THEN / OUTPUT "Coach" / ELSE / OUTPUT "Minibus" / END IF
These three statements are all valid, because the meaning is clear and each one keeps to one style. The last one is not valid, because it mixes styles and is missing THEN, so the reader cannot be sure where the condition ends.
Class activity
Two-path game (no computer). Draw a large diamond on a sheet of paper and write a condition in it, for example ‘Is the card a number below 8?’. Label one path THEN and one path ELSE and put an action on each, such as ‘jump twice’ and ‘turn around’. Use playing cards, number slips or dice. A partner turns over a card; you decide which path it takes, then do the action. Check that exactly one path is taken each time. Then write your game as pseudocode with IF, THEN, ELSE and ENDIF, and write it again in a different valid style. If you have a computer or tablet, draw the flowchart with a drawing program.
Practice
1
Follow the algorithm with 45 seats, then with 30 seats. What is shown each time?
45 seats: 45 >= 30 is true, so Coach is shown. 30 seats: 30 >= 30 is true, so Coach again. With 29 seats it would be Minibus.
2
Write IF ... THEN ... ELSE pseudocode: if the user’s points are at least 25 it shows Winner, otherwise it shows Next time.
INPUT points / IF points >= 25 THEN / OUTPUT "Winner" / ELSE / OUTPUT "Next time" / ENDIF, with the output lines indented.
3
Mia says: With IF ... ELSE the program might run both branches if the condition is nearly true. Is she right? Explain.
No. A condition is only true or false. If it is true the THEN branch runs and the ELSE branch is skipped. If it is false the ELSE branch runs. Exactly one branch runs.
4
This statement has the branches in the wrong order for its purpose. It should show Free entry for children under 5. Find the mistake and fix it.
Pseudocode
1INPUT age
2IF age < 5 THEN
3 OUTPUT "Pay at the desk"4ELSE
5 OUTPUT "Free entry"6ENDIF
The branches are swapped: a child under 5 makes the condition true, but the THEN branch shows Pay at the desk. Swap the two OUTPUT lines (or change the condition to age >= 5).