Year 10 · Computer Science · Summer 1
Programming · Lesson 3 of 5
Strings, operators and library routines
Practical8.1 Programming concepts
Year 10 · Computer Science · Programming · Lesson 3 of 5
You will learn to
- Use the string functions LENGTH, SUBSTRING, UCASE and LCASE.
- Use arithmetic, relational and logical operators, including MOD and DIV.
- Use the library routines ROUND and RANDOM.
- Create a password checker and a dice game in Python and in pseudocode.
Year 10 · Computer Science · Programming · Lesson 3 of 5
Success criteria
You have got it when you can say:
- I can work out 17 MOD 5 and 17 DIV 5.
- I can take the first three letters of a name with SUBSTRING.
- I can simulate a dice roll and round a result to 2 decimal places.
Year 10 · Computer Science · Programming · Lesson 3 of 5
Retrieval starter
Questions from before. Write your answers, then show them.
- Last lesson
A REPEAT … UNTIL loop…
A Runs for everB May run zero timesC Always runs at least once
C. Always runs at least once
- Last unit
A range check on age 0 to 120 would reject…
A 120B 0C 150
C. 150
- Earlier this year
Adding 11001100 and 01011010 in an 8-bit register gives…
A 11111111B 00100110 with no problemC An overflow error
C. An overflow error
Year 10 · Computer Science · Programming · Lesson 3 of 5
Key words (1 of 5)
- Variable
- A named place in memory whose value can change while the program runs.
- Constant
- A named value that is set once and cannot change while the program runs.
- Identifier
- The name given to a variable, constant, array or subroutine.
- Data type
- The kind of value stored: INTEGER, REAL, CHAR, STRING or BOOLEAN.
Year 10 · Computer Science · Programming · Lesson 3 of 5
Key words (2 of 5)
- Assignment
- Storing a value in a variable, written with ← in pseudocode.
- Sequence
- Statements run one after another in the order they are written.
- Selection
- Choosing which statements to run using a condition (IF or CASE).
- Iteration
- Repeating a block of statements in a loop.
Year 10 · Computer Science · Programming · Lesson 3 of 5
Key words (3 of 5)
- Count-controlled loop
- A FOR loop that repeats a set number of times.
- Pre-condition loop
- A WHILE loop that checks its condition before each pass, so it may run zero times.
- Post-condition loop
- A REPEAT UNTIL loop that checks its condition after each pass, so it runs at least once.
- Totalling
- Keeping a running total by adding each value to a total variable.
Year 10 · Computer Science · Programming · Lesson 3 of 5
Key words (4 of 5)
- Counting
- Adding 1 to a counter variable each time something happens.
- Procedure
- A named subroutine that does a task and is run with CALL; it does not return a value.
- Function
- A named subroutine that returns a single value to where it was called.
- Parameter
- A variable in a subroutine heading that receives a value when the subroutine is called.
Year 10 · Computer Science · Programming · Lesson 3 of 5
Key words (5 of 5)
- Local variable
- A variable declared inside a subroutine that can only be used inside it.
- Global variable
- A variable declared in the main program that can be used anywhere in the program.
- Library routine
- A ready-made, tested subroutine provided with the language, such as ROUND or RANDOM.
- Array
- A data structure holding many values of the same data type under one name, each found by its index.
The explanation · step 1 of 23 · part 1 of 3
Variables: named boxes in memory
A variable is a named place in memory that holds one value. The value can change while the program runs.
The name is called an identifier. Cambridge pseudocode writes identifiers in PascalCase, such as `TotalScore`.
`DECLARE` gives the name and the data type before the variable is used.
The arrow `←` means assignment: work out the value on the right, then store it in the variable on the left.
The explanation · step 1 of 23 · part 2 of 3
Variables: named boxes in memory (continued)
DECLARE Score : INTEGER Score ← 0 Score ← Score + 5 OUTPUT Score
score = 0 score = score + 5 print(score)
The explanation · step 1 of 23 · part 3 of 3
Variables: named boxes in memory (continued)
Python does not need a declaration. It works out the type from the value. Cambridge pseudocode declares every variable so the type is clear to the reader.
Read `Score ← Score + 5` from right to left. It is not a maths equation. It means: work out Score + 5, then put the answer back in Score.
The explanation · step 2 of 23 · part 1 of 3
Constants: values that never change
A constant is a named value that is set once. It cannot change while the program runs.
In pseudocode write `CONSTANT`, the name, `←` and the value. No data type is given: the value shows it.
Python has no true constants. Programmers write the name in CAPITALS to mean: do not change this.
The explanation · step 2 of 23 · part 2 of 3
Constants: values that never change (continued)
CONSTANT VATRate ← 0.21 DECLARE Price : REAL Price ← 50.0 OUTPUT "Price with VAT: ", Price * (1 + VATRate)
VAT_RATE = 0.21
price = 50.0
print("Price with VAT:", price * (1 + VAT_RATE))The explanation · step 2 of 23 · part 3 of 3
Constants: values that never change (continued)
If the VAT rate changes, you edit one line instead of every line that uses 0.21. The name also tells the reader what 0.21 means. The program cannot change it by mistake.
Use a constant only for a value that is fixed for the whole run, like a tax rate or the size of an array. A value that the program updates, like a score, must be a variable.
The explanation · step 3 of 23 · part 1 of 4
The five data types
A data type says what kind of value a variable holds. It decides how the value is stored and what you can do with it.
Cambridge uses five data types. A CHAR goes in single quotes. A STRING goes in double quotes.
The explanation · step 3 of 23 · part 2 of 4
The five data types (continued)
| Data type | What it holds | Examples | Python |
|---|---|---|---|
| INTEGER | A whole number: positive, negative or zero | 25, -3, 0 | int |
| REAL | A number with a decimal part | 3.142, -0.5, 19.99 | float |
| CHAR | Exactly one character | 'A', '7', '?' | str of length 1 (no separate char type) |
| STRING | Zero or more characters (text) | "Madrid", "28013", "" | str |
| BOOLEAN | Only TRUE or FALSE | TRUE, FALSE | True, False |
The explanation · step 3 of 23 · part 3 of 4
The five data types (continued)
DECLARE Age : INTEGER DECLARE Height : REAL DECLARE Grade : CHAR DECLARE FirstName : STRING DECLARE IsMember : BOOLEAN Age ← 15 Height ← 1.68 Grade ← 'A' FirstName ← "Lucia" IsMember ← TRUE
The explanation · step 3 of 23 · part 4 of 4
The five data types (continued)
age = 15 height = 1.68 grade = "A" first_name = "Lucia" is_member = True print(type(age), type(height), type(grade), type(first_name), type(is_member))
The type decides which operations make sense. You can add two INTEGERs. You cannot multiply two names.
The explanation · step 15 of 23 · part 1 of 2
Choosing the right loop
First ask: do I know how many times? If yes, use FOR.
If not, ask: must it run at least once? If yes, use REPEAT UNTIL. If it might need to run zero times, use WHILE.
| Loop | Condition checked | Runs at least once? | Use it when | Example |
|---|---|---|---|---|
| FOR (count-controlled) | No condition: it counts | Runs a fixed number of times | You know how many times | Enter 30 marks |
| WHILE (pre-condition) | Before each pass | No, it can run 0 times | Unknown number of times, maybe none | Add sales until the target is reached |
| REPEAT UNTIL (post-condition) | After each pass | Yes, always | Unknown number, but at least one | Ask for a PIN until it is correct |
The explanation · step 15 of 23 · part 2 of 2
Choosing the right loop (continued)
If a question says 'use a pre-condition loop', you must use WHILE. Using a different loop loses marks, even if the program works.
The explanation · 19 more steps
More on the lesson page
The lesson page has the rest of the explanation, step by step:
- Choosing the right data type
- Input and output
- Sequence: one step after another
- Selection with IF
- Nested IF: up to three levels
- Selection with CASE
- Relational and logical operators
- Arithmetic operators, DIV and MOD
- Iteration: the count-controlled FOR loop
- The pre-condition WHILE loop
- The post-condition REPEAT UNTIL loop
- Totalling and counting
- String handling: LENGTH, UCASE, LCASE
- SUBSTRING and the first position: 1 or 0
- Procedures and parameters
- Functions: subroutines that return a value
- Local and global variables
- Library routines: MOD, DIV, ROUND, RANDOM
- Writing maintainable programs
Year 10 · Computer Science · Programming · Lesson 3 of 5
Try it: Test the score checker (0 to 50)
Year 10 · Computer Science · Programming · Lesson 3 of 5
The maths: DIV and MOD
Symbols first, then the words, then the equation. Change the numbers.
Year 10 · Computer Science · Programming · Lesson 3 of 5
Quick quiz · 1 of 3
A function differs from a procedure because it…
- A Has a name
- B Can have parameters
- C Returns a value
C. Returns a value
Both have names and parameters. Only a function gives a value back.
Year 10 · Computer Science · Programming · Lesson 3 of 5
Quick quiz · 2 of 3
A local variable…
- A Exists only inside its subroutine
- B Can be used anywhere
- C Never changes
A. Exists only inside its subroutine
It is created when the subroutine starts and gone when it ends.
Year 10 · Computer Science · Programming · Lesson 3 of 5
Quick quiz · 3 of 3
DECLARE Marks : ARRAY[1:30] OF INTEGER makes…
- A 30 strings
- B 30 whole-number slots, numbered 1 to 30
- C One number
B. 30 whole-number slots, numbered 1 to 30
Cambridge arrays start at 1 unless the question says otherwise.
Year 10 · Computer Science · Programming · Lesson 3 of 5
Exit ticket
Answer on paper before you go. Then check.
SUBSTRING("Computer", 1, 3) gives…
A ComputerB ComC omp
B. Com Start at position 1, take 3 characters. Cambridge strings start at 1.
FOR i ← 1 TO 10 … NEXT i runs the loop…
A 10 timesB 11 timesC 9 times
A. 10 times 1 to 10 inclusive. Python's range(1, 11) does the same.