AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

One question, two roads

Week 2 · 1.2 Selection in pseudocode · 3 lessons
3

This way or that way

Computing 8: Topic 1.2, pp. 27–58
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.

StartINPUT ageage >= 60?yesnoprice ← 4price ← 7OUTPUT priceEnd
Worked examples
Pseudocode
1INPUT days
2IF days > 0 THEN
3    cost ← days * 2
4    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 ← 4
4ELSE
5    price ← 7
6ENDIF
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.
StyleStatement
1IF seats >= 30 THEN / OUTPUT "Coach" / ELSE / OUTPUT "Minibus" / ENDIF
2if seats >= 30 then / output "Coach" / else / output "Minibus" / endif
3IF seats >= 30 THEN / OUTPUT "Coach" / ELSE / OUTPUT "Minibus" / END IF
not validIF seats >= 30 / OUTPUT "Coach" / else / OUTPUT "Minibus" / ENDIF
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?
Pseudocode
1INPUT seats
2IF seats >= 30 THEN
3    OUTPUT "Coach"
4ELSE
5    OUTPUT "Minibus"
6ENDIF
2
Write IF ... THEN ... ELSE pseudocode: if the user’s points are at least 25 it shows Winner, otherwise it shows Next time.
3
Mia says: With IF ... ELSE the program might run both branches if the condition is nearly true. Is she right? Explain.
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