In your own wordsQuestion 1 · Apply
Loading…
By the end you can turn a flowchart or structured English design into Cambridge pseudocode. You can declare variables and constants, write assignments and expressions, use INPUT and OUTPUT, and use built-in functions such as LENGTH and LEFT.
A design tells you what a program must do. It can be a flowchart, or a list of steps written in plain English called structured English. Your job is to turn it into pseudocode, one line at a time.
Work in four steps:
Each flowchart shape has a matching pseudocode form:
| Flowchart shape | Pseudocode |
|---|---|
| Start / End | Not written. The program starts at the top and stops at the bottom. |
| Input (parallelogram) | INPUT |
| Output (parallelogram) | OUTPUT |
| Process (rectangle) | An assignment, such as Total ← Total + Price |
| Decision (diamond) | IF … THEN … ELSE … ENDIF |
| Loop (a decision diamond with an arrow back to an earlier step) | FOR … NEXT, WHILE … ENDWHILE or REPEAT … UNTIL |
A variable is a named box in memory that holds a value, and the value can change. Before you use a variable, you declare it: you give its name and its data type.
DECLARE Count : INTEGER
DECLARE Name : STRING
DECLARE Price : REAL
The data type says what kind of value the box can hold:
| Data type | Holds | Example value |
|---|---|---|
INTEGER |
a whole number | 7 |
REAL |
a number that can have a decimal part | 2.50 |
CHAR |
a single character | 'A' |
STRING |
text of any length | "Ali" |
BOOLEAN |
true or false | TRUE |
Quote marks matter. A CHAR literal uses single quotes, such as 'A'. A STRING literal uses double quotes, such as "Ali".
Declaring a variable does not give it a value. Give it a value on a separate line, with an assignment:
DECLARE Count : INTEGER
Count ← 0
A constant is a name for a value that never changes. You write the value itself (a literal) straight after the =, and you never assign it again:
CONSTANT MaxScore = 100
Identifiers (the names you choose) use PascalCase: each word starts with a capital letter, with no spaces, such as TotalScore. Keywords such as DECLARE and INPUT are written in capitals.
Assignment puts a value into a variable. Its symbol is an arrow, ←:
Total ← Total + Price
Read the arrow as "becomes", not "equals". The right-hand side is worked out first, and the answer is stored in Total. If Total was 10 and Price is 4, then Total becomes 14.
An expression is a calculation that gives one value. These are the operators you need:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+, -, * |
add, subtract, multiply | 7 * 3 |
21 |
/ |
divide (always gives a REAL) | 7 / 2 |
3.5 |
DIV |
whole-number division, dropping the remainder | 7 DIV 2 |
3 |
MOD |
the remainder after division | 7 MOD 2 |
1 |
=, <> |
equal to, not equal to | Age = 16 |
TRUE or FALSE |
<, >, <=, >= |
less than, greater than, and the "or equal" versions | Age >= 18 |
TRUE or FALSE |
AND, OR, NOT |
combine or reverse TRUE/FALSE values | (Age >= 18) AND (Age < 65) |
TRUE or FALSE |
& |
joins two strings | "Ali" & " Khan" |
"Ali Khan" |
Notice that = is a comparison here, not an assignment. Use ← to store a value, and = to ask whether two values are equal.
INPUT reads one value from the user and stores it in a variable:
OUTPUT "Enter your name"
INPUT Name
Always show a prompt with OUTPUT before an INPUT, so the user knows what to type. INPUT stores the value, but it prints nothing.
OUTPUT shows values on the screen. Separate the items with commas:
OUTPUT "Hello, ", Name
OUTPUT "Total: ", Total
Items are joined exactly as written, with no extra spaces added. If you want a space, put it inside the quotes, as in "Hello, " above.
Worked example: Worked example — sum of the even numbers from 1 to 10
Design: start with Total at 0. For each number Count from 1 to 10, if Count is even, add it to Total. Finally, output Total.
Work out the answer first: 2 + 4 + 6 + 8 + 10 = 30. Your pseudocode must print 30.
This program asks for a first name and a surname, joins them with a space, and shows the length of the full name. It has one bug.
Ali and Khan it prints AliKhan, which is missing the space.Ali Khan and Length: 8.Remember: & joins strings, and LENGTH counts every character, including the space.
DECLARE FirstName : STRING
DECLARE Surname : STRING
DECLARE FullName : STRING
DECLARE NameLength : INTEGER
OUTPUT "Enter your first name"
INPUT FirstName
OUTPUT "Enter your surname"
INPUT Surname
// Join FirstName, a space and Surname into FullName
FullName ← FirstName & Surname
NameLength ← LENGTH(FullName)
OUTPUT FullName
OUTPUT "Length: ", NameLength
Open this lesson on a computer to edit and run this code and check your answer.