HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
Variables, Selection and Loops in Pseudocode
  1. 1Variables, constants, data types, input and output
  2. 2Selection with IF and CASE
  3. 3Loops, totals, counts and nested statements
  1. Computer Science
  2. /Variables, Selection and Loops in Pseudocode
  3. /Selection with IF and CASE

Course 8.1·Lesson 2 of 3

Selection with IF and CASE

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.

about 25 min1 simulation1 practice question
Lesson 2 of 3Show all lessonsHide
Variables, Selection and Loops in Pseudocode
  1. 1Variables, constants, data types, input and output
  2. 2Selection with IF and CASE
  3. 3Loops, totals, counts and nested statements

Try the code and the simulations beside the lesson.

Choosing a path

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.

The IF statement

Here is the cinema choice in pseudocode:

pseudocode
DECLARE Age : INTEGER
INPUT Age
IF Age < 12
  THEN
    OUTPUT "Child ticket"
  ELSE
    OUTPUT "Adult ticket"
ENDIF

Look 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.

A flowchart. Start, then a parallelogram INPUT Age, then a diamond Age < 12 ?. The Yes exit goes left to a parallelogram OUTPUT "Child ticket". The No exit goes right to a parallelogram OUTPUT "Adult ticket". Both paths join and go down to Stop.

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.

Nested IF statements

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:

pseudocode
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 ", Grade

The tests run from the top. A mark of 727272 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 858585.

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.

The CASE statement

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:

pseudocode
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 Position

Each 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.

Practise with new numbers

Try it in Python

The program turns marks into grades with if, elif and else. Then it writes the CASE statement of the game as if and elif.

Read it first. Then run it. Which marks sit exactly on a boundary?

1. Change >= to > in the test for 65. Which line of the output changes?

2. Set move = "Q". What does the program print now, and which pseudocode keyword does that branch stand for?

Common mistakes

  • Writing a condition in words. IF Mark is more than 50 scores nothing. Write IF Mark > 50.
  • Testing the bands in the wrong order. Test the highest band first. If you test Mark >= 50 first, a mark of 909090 stops there.
  • > where >= is meant. A boundary mark such as 808080 then falls into the wrong band. Always test the boundary values.
  • OTHERWISE in the middle. It must be the last case.
  • A missing ENDIF. Every IF needs its own ENDIF, and a nested IF needs one too.

Remember these

  • IF <condition>, then THEN, an optional ELSE, then ENDIF.
  • THEN, ELSE and each case are indented by two spaces.
  • A CASE statement tests its cases in order and runs only the first match. OTHERWISE comes last.
  • Use CASE for one variable against single values, and IF for ranges and joined conditions.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. In the grade program, which grade does a mark of exactly 505050 get?
  1. Write a condition that is TRUE when Age is from 13 to 19, both included.
  1. In the game, what is output for the input 'D'?
  1. Why is a CASE statement a poor choice for the grade program?
  1. Find the mistake: IF Total <> 0 THEN OUTPUT "Done" ENDIF ELSE OUTPUT "Empty".

Enroll free in Variables, Selection and Loops in Pseudocode

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

A café screen shows three meals: 1 for soup, 2 for pasta and 3 for salad…Easy · High School (9–10) · by StemPy Lab
A car park charges by the whole hour. This pseudocode works out the charge in dollars…Medium · High School (9–10) · by StemPy Lab
A youth club accepts members aged 11 to 16, both included. A programmer wrote…Hard · High School (9–10) · by StemPy Lab
Next: lesson 3Loops, totals, counts and nested statements
← Variables, Selection and Loops in Pseudocode
← Variables, constants, data types, input and outputLoops, totals, counts and nested statements →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place in the course.

Python Labs (1)

Grades with selectionPython Lab · by StemPy Lab