Course 8.1Lesson 2 of 3
A program chooses what to do next by testing a condition. This lesson writes that choice in Cambridge pseudocode with IF statements, nested IF statements and CASE statements, and shows the same choice in Python.
Try the code and the simulations beside the lesson.
A cinema charges children less than adults. The ticket program must choose. This is selection: the program tests a condition, and the result decides which statements run. You met it in Python in the Foundations chapter "Making Decisions", with if, elif and else.
Here is the cinema choice in pseudocode:
DECLARE Age : INTEGER
INPUT Age
IF Age < 12
THEN
OUTPUT "Child ticket"
ELSE
OUTPUT "Adult ticket"
ENDIFLook at the layout, because the syllabus fixes it. THEN and ELSE sit two spaces in from IF. The statements inside them sit two more spaces in. ENDIF lines up with IF. An IF statement may have no ELSE at all: then nothing happens when the condition is FALSE.
The condition must give a BOOLEAN. You can join conditions with AND, OR and NOT. A mark is out of range when Mark < 0 OR Mark > 100. Brackets make a long condition clear.
The flowchart shows the same IF statement. The diamond is the decision. Its two exits, Yes and No, are the two branches. Both branches meet again before the program goes on.
One IF statement can sit inside another. This is a nested statement. Each new level is two more spaces in. Here a mark is turned into a grade:
DECLARE Mark : INTEGER
DECLARE Grade : CHAR
INPUT Mark
IF Mark >= 80
THEN
Grade ← 'A'
ELSE
IF Mark >= 65
THEN
Grade ← 'B'
ELSE
IF Mark >= 50
THEN
Grade ← 'C'
ELSE
Grade ← 'U'
ENDIF
ENDIF
ENDIF
OUTPUT "Grade ", GradeThe tests run from the top. A mark of fails the first test, 72 >= 80, so the program goes to the ELSE branch. There it passes the second test, 72 >= 65, and the grade is B. The inner tests are never reached for a mark of .
The exam does not ask you to write more than three levels of nesting. Python writes this choice with elif, which saves the extra levels.
When one variable decides between several single values, a CASE statement is shorter. A game moves a counter with the keys W, E, A and D:
DECLARE Move : CHAR
DECLARE Position : INTEGER
Position ← 50
INPUT Move
CASE OF Move
'W' : Position ← Position - 10
'E' : Position ← Position + 10
'A' : Position ← Position - 1
'D' : Position ← Position + 1
OTHERWISE OUTPUT "Beep"
ENDCASE
OUTPUT PositionEach case is two spaces in. The cases are tested in order. When one matches, its statement runs and the CASE statement is finished: the other cases are not tested. OTHERWISE, if it is there, must be the last case. It runs when no case matches. With the input 'A', the output is 49. With 'X', the program outputs Beep and then 50.
Keep each case to one statement and one value. When the choice is more complex, such as a range of marks, use an IF statement.
The program turns marks into grades with if, elif and else. Then it writes the CASE statement of the game as if and elif.
IF Mark is more than 50 scores nothing. Write IF Mark > 50.Mark >= 50 first, a mark of stops there.> where >= is meant. A boundary mark such as then falls into the wrong band. Always test the boundary values.ENDIF. Every IF needs its own ENDIF, and a nested IF needs one too.IF <condition>, then THEN, an optional ELSE, then ENDIF.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Age is from 13 to 19, both included.'D'?IF Total <> 0 THEN OUTPUT "Done" ENDIF ELSE OUTPUT "Empty".Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.