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
2

Do it only if

Computing 8: Topic 1.2, pp. 27–58
GoalWrite and follow an IF ... THEN ... ENDIF statement and use indentation to show which lines are inside it.
New words
IF statement · a selection that runs some lines only when a condition is trueENDIF · the command word that marks the end of an IF statementindentation · moving lines to the right so you can see which ones are inside a structure
Explanation

The simplest selection is IF ... THEN ... ENDIF. The first line is IF, then the condition, then THEN. Next come the lines to carry out for a true condition. ENDIF closes the statement. When the condition is not true, the lines inside are skipped and the program carries on after ENDIF. The lines inside are indented, which means moved a few spaces to the right. Indentation does not change what the algorithm does, but it shows where the IF statement starts and ends, so the algorithm is easy to follow. Lines after ENDIF run every time.

Pseudocode
1INPUT age
2IF age < 14 THEN
3    OUTPUT "Please wear a helmet"
4ENDIF
Worked examples
StartINPUT ageage < 14?yesnoOUTPUT "Please wear a helmet"End
A bike shop reminds young riders about helmets. The condition is age < 14. With 12 typed the message is shown. With 20 typed nothing is shown at all, because there is no else part.
Pseudocode
1INPUT distance
2fare ← 3
3IF distance > 5 THEN
4    fare ← fare + 2
5ENDIF
6OUTPUT "Fare: ", fare
An IF can change a variable. The fare starts at 3 and rises by 2 only for long trips. For a distance of 8 the output is Fare: 5. For a distance of 5 it is Fare: 3, because 5 > 5 is false.
Pseudocode
1INPUT books
2IF books > 3 THEN
3    OUTPUT "Ask a librarian"
4ENDIF
5OUTPUT "Thank you"
Indentation shows which lines are inside the IF. The line Thank you is not indented and is after ENDIF, so it runs every time. With 5 books both lines are shown. With 2 books only Thank you is shown.
Class activity

Act it out (no computer). Write a short IF statement on a sheet of paper, for example IF cards >= 10 THEN the player claps ENDIF. Put a line of tape or a ruler on the floor for ENDIF. Your partner picks a number. You say whether the condition is true. If it is true you step inside the lines and do the action; if it is false you step straight past ENDIF. Then add a line after ENDIF that always happens, such as saying ‘Done’. Try the numbers 4, 10 and 15. Swap roles and write a new IF statement. If you have a computer or tablet, type one of your statements in Python afterwards.

Practice
1
The user types 75, then 60. Follow the algorithm and write what is shown each time.
Pseudocode
1INPUT mins
2IF mins >= 60 THEN
3    OUTPUT "Long call"
4ENDIF
5OUTPUT "Call ended"
2
Write an IF statement for this: the user types the number of points. If the points are over 100, the program shows Level up.
3
This algorithm should add a 1 coin charge for bags that weigh more than 20 kilos. It has two mistakes. Find them.
Pseudocode
1INPUT kilos
2fee ← 0
3IF kilos > 20 THEN
4fee ← 1
5OUTPUT "Fee: ", fee
4
Explain what is different about the two lines Ask a librarian and Thank you in the books example. When does each run?