← 💻 Computer Science

Cambridge IGCSE 0478 · Topic 8 of 10

Programming

Writing programs using variables, data types, selection, loops, string handling, subroutines, arrays and files, in the Cambridge pseudocode style.

📌 Key points

  1. A variable stores a value that can change while the program runs; a constant stores a value that does not change (e.g. CONSTANT Pi ← 3.142); identifiers should be meaningful. Assignment uses ←.
  2. Data types: INTEGER (whole number), REAL (number with a decimal part), CHAR (single character), STRING (text) and BOOLEAN (TRUE or FALSE).
  3. The three basic constructs are sequence, selection (IF … THEN … ELSE … ENDIF, and CASE OF … OTHERWISE … ENDCASE) and iteration (loops).
  4. Loops: FOR … TO … NEXT is count-controlled (fixed number of repeats); REPEAT … UNTIL is post-condition (always runs at least once); WHILE … DO … ENDWHILE is pre-condition (may not run at all).
  5. Arithmetic: + − * / plus MOD (remainder, 17 MOD 5 = 2) and DIV (whole-number division, 17 DIV 5 = 3); ROUND(Value, Places) rounds and RANDOM() gives a random number between 0 and 1.
  6. String handling: LENGTH(S) gives the number of characters, SUBSTRING(S, Start, Length) extracts part of a string, UCASE and LCASE change to upper or lower case.
  7. Procedures and functions are named subroutines that can take parameters; a function returns a value. Local variables exist only inside their subroutine; global variables can be used anywhere in the program.
  8. An array stores many values of the same data type under one identifier, using an index: DECLARE Scores : ARRAY[1:30] OF INTEGER; a 2D array has rows and columns, e.g. ARRAY[1:3, 1:4]; arrays are often processed with FOR loops.
  9. Files store data permanently: OPENFILE "Data.txt" FOR READ (or WRITE), READFILE and WRITEFILE to read or write a line, then CLOSEFILE.
  10. Maintainable programs use meaningful identifiers, comments (// …), indentation, and procedures and functions so the code is easy to understand and update.

📖 Key terms

Variable
A named memory location that stores a value that can change while the program runs.
Constant
A named value that cannot change while the program runs.
Count-controlled loop
A loop that repeats a set number of times (FOR … NEXT).
Post-condition loop
A loop whose condition is checked at the end, so it runs at least once (REPEAT … UNTIL).
Function
A subroutine that returns a single value to the code that called it.
Parameter
A value passed into a procedure or function when it is called.
Local variable
A variable declared in a subroutine that can only be used in that subroutine.
Array
A data structure holding several items of the same data type under one identifier, accessed by index.

✏️ Worked example

Write pseudocode to input 10 numbers and output their total and average.

  1. Set the total to zero before the loop: Total ← 0
  2. Use a count-controlled loop: FOR Count ← 1 TO 10
  3. Inside the loop input each number and add it to the total: INPUT Number then Total ← Total + Number
  4. Close the loop: NEXT Count
  5. After the loop: Average ← Total / 10
  6. Output both: OUTPUT Total, Average
💡 Exam tip

In pseudocode answers, initialise totals and counters before the loop and output after the loop. Putting OUTPUT inside the loop, or Total ← 0 inside it, are very common mistakes.

Test yourself8 questions from a bank of 17. A new mix every time.
Start the quiz →

It’s bedtime! 😴

It’s late, and even Numo is getting sleepy. The best thing you can do for your learning now is… go to sleep!

✨ Sleep helps you

  • 🧠 remember what you learned today: your brain saves it while you sleep
  • 🌱 grow and stay healthy
  • ⚡ wake up with energy, ready to learn and play
  • 😊 feel happy and calm

🦉 Going to bed late can make you

  • 😩 tired and sleepy at school tomorrow
  • 🤯 find it harder to focus and to remember
  • 😠 grumpy and easily upset
  • 🤧 catch colds more easily

Children your age need 9 to 12 hours of sleep every night. Numo will be right here tomorrow!