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. /Variables, constants, data types, input and output

Course 8.1·Lesson 1 of 3

Variables, constants, data types, input and output

In the written exam a program is answered in pseudocode, not in a real language. This lesson shows the Cambridge way of writing variables, constants, the five data types, the operators, input and output, beside the Python you may know already.

about 30 min1 simulation1 practice question
Lesson 1 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.

Why learn pseudocode at all?

Think of a ticket machine at a museum. It asks how many children are in your group, works out the price and prints it. Somebody wrote that program in a real language. Before that, they planned the steps on paper.

Pseudocode is that plan, written in a fixed and careful way. It looks like a program, but no computer runs it. A person reads it. In Paper 2 of Cambridge IGCSE Computer Science, every answer that needs code is written in pseudocode. An answer in a real programming language scores no marks, except in the long scenario question at the end, where Python, Visual Basic or Java are also allowed.

So why does this site keep Python beside it? Because a computer can run Python. On this page, every pseudocode block is shown as plain text, and the same algorithm sits in a Python box that you can run. Use the Python to check what the pseudocode does.

If you have worked through the Python Foundations book on this site, you already know most of the ideas: variables, input(), if, loops, lists and functions. Here you learn how the exam writes them, and the exam habits that go with them.

The house rules of Cambridge pseudocode

The syllabus fixes how pseudocode looks. Examiners use these rules, and so should you.

  • Keywords are in capitals: DECLARE, IF, OUTPUT, REPEAT.
  • Identifiers (the names of variables, constants, procedures and functions) use capital letters to start each word, such as NumberOfPlayers. They contain only letters and digits, start with a capital letter, and never use an underscore or a keyword.
  • Indentation is four spaces for the lines inside a statement.
  • Comments start with // and run to the end of the line.
  • Precision matters. Write X > Y, not "X is greater than Y". The second one scores nothing.

Data types, literals and variables

Every value has a data type. The syllabus uses five of them.

  • INTEGER: a whole number, such as 5 or -3.
  • REAL: a number that can have a fractional part. A real literal always has a digit on both sides of the point: 4.7, 0.3, -4.0.
  • CHAR: one character, in single quotes: 'x', '@'.
  • STRING: a sequence of zero or more characters, in double quotes: "This is a string". The empty string is "".
  • BOOLEAN: one of the two values TRUE and FALSE.

A variable is a named place in memory whose value can change while the program runs. You declare it with its type:

pseudocode
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN

A constant is a named value that never changes while the program runs. It makes a program easier to read, and easier to update: change one line and every use of the constant follows. A constant takes only a literal value, never a variable or a sum:

pseudocode
CONSTANT HourlyRate ← 6.50
CONSTANT DefaultText ← "N/A"

The arrow ← is assignment. Read Counter ← Counter + 1 as "Counter becomes Counter plus one". The equals sign = is kept for comparing two values.

Input, output and the operators

INPUT Answer waits for a value and stores it in the variable Answer. OUTPUT shows one or more values, separated by commas, with nothing added between them:

pseudocode
OUTPUT "You have ", Lives, " lives left"

The arithmetic operators are +, -, *, / and ^ (raised to the power of). Two more work on integers. DIV(10, 3) returns 3, the whole number of times 333 fits into 101010. MOD(10, 3) returns 1, the remainder. You also see them written between the numbers: 10 DIV 3 and 10 MOD 3. Multiplication and division are done before addition and subtraction. Brackets make the order clear.

The relational operators compare two values and give a BOOLEAN: =, <, <=, >, >=, and <> for "not equal to". The logical operators are AND, OR and NOT. They join BOOLEAN values.

A whole program: the school trip

One child in every group of five goes free. Each other child pays 4 dollars. Read the program line by line. This is a sequence: the statements run one after the other, from the top.

pseudocode
// Tickets for a school trip: one child in every five goes free
CONSTANT ChildPrice ← 4
CONSTANT GroupSize ← 5
DECLARE Children : INTEGER
DECLARE FreeTickets : INTEGER
DECLARE ToPay : INTEGER
OUTPUT "How many children?"
INPUT Children
FreeTickets ← DIV(Children, GroupSize)
ToPay ← (Children - FreeTickets) * ChildPrice
OUTPUT "Free tickets: ", FreeTickets
OUTPUT "Total to pay: ", ToPay

Suppose the input is 23. DIV(23, 5) is 4, so four tickets are free. The other 191919 children pay 19×4=7619 \times 4 = 7619×4=76 dollars. The program outputs Free tickets: 4 and then Total to pay: 76.

How does this look in Python? The arrow ← becomes =. The comparison = becomes ==, and <> becomes !=. ^ becomes **, DIV becomes //, and MOD becomes % (for positive numbers). OUTPUT becomes print(), and INPUT becomes input() with int() around it for a number. You met all of these in the Python Foundations chapters "Numbers and Operators" and "Ask and Answer: input()".

Practise with new numbers

Try it in Python

The program is the school trip again, in Python, with the number of children fixed at 23 instead of typed in. It also prints the data type of four values.

Read it first. Then run it. Do the two totals match the pseudocode above?

1. Change children = 23 to children = 25. How many tickets are free now, and why did the total not go up by 8?

2. Python calls a REAL value a float and a BOOLEAN a bool. Find them in the last line of the output.

Dry-running a program

In the exam you often follow a program by hand, line by line, and write down each value. This is a dry run. Try this one, which swaps two values:

pseudocode
DECLARE First : INTEGER
DECLARE Second : INTEGER
DECLARE Temp : INTEGER
First ← 8
Second ← 3
Temp ← First
First ← Second
Second ← Temp
OUTPUT First, " ", Second

Temp takes 8. Then First takes 3. Then Second takes the 8 kept in Temp. The output is 3 8. Without Temp, the first value would be lost.

Common mistakes

  • Writing = for assignment. In pseudocode, assignment is ←. The sign = compares.
  • A real literal without a digit on each side. Write 4.0 and 0.5, never 4. or .5.
  • Quotes on the wrong type. A CHAR has single quotes, a STRING has double quotes, and a number has none. "5" is a string, not an integer.
  • A constant set from a variable. CONSTANT Rate ← Price is not allowed. A constant takes only a literal.
  • Mixing up DIV and MOD. DIV gives the whole groups, MOD gives what is left over.

Remember these

  • Five data types: INTEGER, REAL, CHAR, STRING, BOOLEAN.
  • DECLARE Name : TYPE makes a variable. CONSTANT Name ← value makes a constant.
  • ← assigns. = and <> compare.
  • DIV(A, B) is the whole part of A÷BA \div BA÷B. MOD(A, B) is the remainder.

Check yourself

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

  1. Which data type suits each value: the number of goals in a match, the height of a student in metres, and whether a door is locked?
  1. What do DIV(17, 5) and MOD(17, 5) return?
  1. Find the mistake: CONSTANT Total ← Price * 2.
  1. X ← 4 and then X ← X ^ 2 - 1. What is the value of X now?
  1. Is "7" the same as 7 in pseudocode?

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 school library keeps a record for each book…Easy · High School (9–10) · by StemPy Lab
A stopwatch app stores a time as a number of seconds…Medium · High School (9–10) · by StemPy Lab
A student wrote this pseudocode to find the area of a floor in square metres and the number of…Medium · High School (9–10) · by StemPy Lab
Next: lesson 2Selection with IF and CASE
← Variables, Selection and Loops in Pseudocode
← PreviousSelection with IF and CASE →

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.