Read the full lesson
The complete written notes for this level.
1. What is pseudocode?
Pseudocode is a simple way to plan a computer program before writing real code.
A set of steps used to solve a problem.
A way to write those steps using normal language mixed with simple program-like instructions.
An algorithm describes the solution. Pseudocode writes that solution step by step so the logic is clear before real code is written.
Use the visual below to see where pseudocode fits between a problem and real code.
2. Data types
A data type tells the program what kind of value it is storing.
Different values need suitable data types, just like different items need suitable containers. Study the picture first, then apply the same idea to data.
Now apply the same idea to pseudocode: each data type is suited to a particular kind of value.
3. Variables and constants
Programs store values in named places called variables.
The data used by the program.
A named place where a value can be stored.
A named value that must stay fixed.
Use the visual to see how each variable name is connected to the value it stores.
"Alice"StudentNameStudentName ← "Alice"The left arrow ← means “stores into”. The value on the right is stored in the variable on the left.
4. Declaring variables
Before a variable is used in a formal pseudocode program, it can be declared.
A declaration gives the variable name and the data type it will store. Click any pseudocode line to see what it does.
These lines create the variable names and say what type of value each one should store.
A constant is used when a named value should stay fixed.
= is used here because the constant is being defined with a fixed value. This is different from ←, which stores a new value into a variable.
After declaration, the program can store values in the variables.
//.The example below shows a comment before a declaration line.
DECLARE gives the variable name and data type. The left arrow ← stores a value into a variable.5. Changing variable values
A variable can store one value now and a different value later.
When a new value is stored in a variable, the old value is replaced. Sometimes the old value is used to calculate the new one.
Score = 10Lives = 3Score = 10 + 5so
Score = 15Lives = 3 - 1so
Lives = 2Score = 15 + Bonusso
Score = 17Read the table from top to bottom. Each row shows what has changed after one assignment statement.
| Step | Statement | Value after the statement |
|---|---|---|
| 1 | Score ← 10 | Score = 10 |
| 2 | Lives ← 3 | Lives = 3 |
| 3 | Score ← Score + 5 | Score = 15 |
| 4 | Lives ← Lives - 1 | Lives = 2 |
| 5 | Bonus ← 2 | Bonus = 2 |
| 6 | Score ← Score + Bonus | Score = 17 |
6. Input and output
INPUT brings data into a program. OUTPUT shows information from the program.
INPUTThe user enters data into the program.
OUTPUTThe program shows information on the screen.
The example below asks for a name, stores the typed value in UserName, and then displays a greeting.
UserName, then reused in the final OUTPUT.7. Arithmetic operations
Arithmetic operations allow a program to perform calculations.
Before reading the full code, use this guide to understand the symbols.
| Symbol | Meaning |
|---|---|
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide |
^ | Power |
( ) | Calculate this part first |
Sum ← Number1 + Number2, the calculation on the right is completed first. The answer is then stored in the variable on the left.
The code below uses Number1 and Number2 to calculate and store results.
| Expression | With values | Result |
|---|---|---|
Number1 + Number2 | 8 + 2 | 10 |
Number1 - Number2 | 8 - 2 | 6 |
Number1 * Number2 | 8 * 2 | 16 |
Number1 / Number2 | 8 / 2 | 4.0 |
Number1 ^ Number2 | 8 ^ 2 | 64 |
(Number1 + Number2) * Number2 | (8 + 2) * 2 | 20 |
8. DIV and MOD
DIV and MOD are both related to division, but they do not give the same result.
DIVGives the whole-number part of a division: the number of full groups.
MODGives the remainder: the part left over.
23 items in groups of 5Given values: TotalItems stores 23, and ItemsPerBox stores 5.
Using DIV: 23 DIV 5 gives 4, because five goes into twenty-three four full times.
Using MOD: 23 MOD 5 gives 3, because three items are left after making four full groups of five.
The grouping visual below shows the full groups and the leftover items.
23 DIV 5 = 4 and 23 MOD 5 = 3.9. ROUND and RANDOM
ROUND and RANDOM are useful routines in arithmetic work, but they do different jobs.
ROUNDUsed when we want to round a number and control how many decimal places are kept.
RANDOMUsed when we want the program to generate a random whole number in a given range.
Use the visual to compare ROUND with RANDOM before reading the pseudocode.
ROUND: change decimal places
19.9876→19.99RANDOM: choose a whole number
ROUND(19.9876, 2) gives 19.99; RANDOM(1, 6) can give any whole number from 1 to 6.10. Relational operations
A relational operation compares two values.
It asks a question, and the answer is always TRUE or FALSE.
← stores a value into a variable. = compares two values and asks whether they are equal.| Symbol | Job | Example | Meaning |
|---|---|---|---|
← | Assignment | Score ← 10 | Store 10 in Score. |
= | Comparison | 10 = 10 | Ask whether the two values are equal. The answer is TRUE. |
| Operator | Meaning | Example | Result |
|---|---|---|---|
= | Equal to | 10 = 10 | TRUE |
< | Less than | 3 < 7 | TRUE |
<= | Less than or equal to | 8 <= 8 | TRUE |
> | Greater than | 9 > 12 | FALSE |
>= | Greater than or equal to | 6 >= 2 | TRUE |
<> | Not equal to | 5 <> 5 | FALSE |
TRUE or FALSE.11. Logical operators
A logical operation works with TRUE and FALSE values.
Logical operators can combine Boolean values or reverse one Boolean value.
ANDboth parts must be true
ORat least one part must be true
NOTthe value is reversed
| Operator | Meaning | Example | Result |
|---|---|---|---|
AND | Needs both parts to be true. | TRUE AND TRUE | TRUE |
AND | If one part is false, the result is false. | TRUE AND FALSE | FALSE |
OR | Needs at least one part to be true. | TRUE OR FALSE | TRUE |
OR | If no part is true, the result is false. | FALSE OR FALSE | FALSE |
NOT | Reverses the value. | NOT TRUE | FALSE |
NOT | Reverses the value. | NOT FALSE | TRUE |
AND needs both parts, OR needs at least one part, and NOT reverses the value.12. Logical operators in pseudocode
Relational comparisons can create TRUE or FALSE results, and logical operators can combine or reverse those results.
The example below uses a student’s age and ticket status. Click any line to see how the result is worked out.
Age >= 12isTRUE, andHasTicket = TRUEisTRUE, soANDgivesTRUE.Age < 12isFALSE, andHasTicket = FALSEisFALSE, soORgivesFALSE.HasTicketisTRUE, soNOT HasTicketgivesFALSE.
TRUE or FALSE results. Logical operators combine or reverse those results.13. Main parts of a simple pseudocode program
A simple pseudocode program can be understood by looking for five parts.
Gives each variable a name and a data type.
Gives a variable a starting value before it is used.
Stores values entered by the user.
Calculates and stores a result.
Shows messages or results on the screen.
This complete example asks for a name and two numbers, adds the numbers, and outputs a greeting and the total.