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
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 ← 33IF distance > 5 THEN
4 fare ← fare + 25ENDIF
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"
75: 75 >= 60 is true, so it shows Long call and then Call ended. 60: 60 >= 60 is true, so the same two lines. If the user typed 59 the output would be only 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.
INPUT points / IF points > 100 THEN / (indented) OUTPUT "Level up" / ENDIF. The line inside the IF must be indented and the statement must end with ENDIF.
3
This algorithm should add a 1 coin charge for bags that weigh more than 20 kilos. It has two mistakes. Find them.
Line 4 is not indented, so it is not clear that it belongs inside the IF. ENDIF is missing, so the statement is never closed. The corrected version indents the fee line and puts ENDIF after it.
4
Explain what is different about the two lines Ask a librarian and Thank you in the books example. When does each run?
Ask a librarian is inside the IF, so it runs only when books > 3 is true. Thank you comes after ENDIF, so it runs every time, whatever the condition is.