Course 8.1Lesson 1 of 3
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.
Try the code and the simulations beside the lesson.
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 syllabus fixes how pseudocode looks. Examiners use these rules, and so should you.
DECLARE, IF, OUTPUT, REPEAT.NumberOfPlayers. They contain only letters and digits, start with a capital letter, and never use an underscore or a keyword.// and run to the end of the line.X > Y, not "X is greater than Y". The second one scores nothing.Every value has a data type. The syllabus uses five of them.
5 or -3.4.7, 0.3, -4.0.'x', '@'."This is a string". The empty string is "".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:
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEANA 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:
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 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:
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 fits into . 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.
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.
// 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: ", ToPaySuppose the input is 23. DIV(23, 5) is 4, so four tickets are free. The other children pay 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()".
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.
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:
DECLARE First : INTEGER
DECLARE Second : INTEGER
DECLARE Temp : INTEGER
First ← 8
Second ← 3
Temp ← First
First ← Second
Second ← Temp
OUTPUT First, " ", SecondTemp 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.
= for assignment. In pseudocode, assignment is ←. The sign = compares.4.0 and 0.5, never 4. or .5."5" is a string, not an integer.CONSTANT Rate ← Price is not allowed. A constant takes only a literal.DECLARE Name : TYPE makes a variable. CONSTANT Name ← value makes a constant.← assigns. = and <> compare.DIV(A, B) is the whole part of . MOD(A, B) is the remainder.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
DIV(17, 5) and MOD(17, 5) return?CONSTANT Total ← Price * 2.X ← 4 and then X ← X ^ 2 - 1. What is the value of X now?"7" the same as 7 in pseudocode?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.