Cambridge Lower Secondary CheckpointStage 9

Programming

Computing Stage 9 Chapter Notes

What this chapter covers

Programming
ShareWhatsAppPost
Programming notes

Unable to load PDF

The notes viewer could not load. Please refresh the page.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Programming notes as text: skim, search, and jump between subtopics.

~12 min read

1. High-Level vs. Low-Level Languages

Programming languages are the tools we use to write instructions for a computer. They fall into two main categories. High-Level Languages (HLLs) like Python or Java use English-like commands and are easy for humans to read, write, and debug. They handle complex tasks with simple statements. Low-Level Languages (LLLs) are much closer to the computer's native language. This category includes Assembly Language and Machine Code (binary 1s and 0s). LLLs give programmers direct control over the hardware but are much harder to work with and are specific to a particular CPU architecture.

Key term

Portability: The ability of a program to be run on different types of computer systems and processors without needing to be rewritten.

Examiner insight

Examiners look for clear comparisons. When asked to compare HLL and LLL, provide a point (e.g., readability) and explain how it applies to both language types.

Common pitfall

Confusing assembly language with machine code. Assembly language uses mnemonics (like ADD, MOV) and is then translated into machine code (binary).

Fun fact

The first popular high-level language was FORTRAN, developed in the 1950s for scientific and engineering calculations. Many of its core principles are still in use today.

Worked example 14 marks

A programmer writes the high-level instruction `total = 10 + 5`. Describe the low-level steps the CPU might take to execute this.

  1. 1

    Step 1: The instruction `total = 10 + 5` is translated into multiple machine code instructions.

  2. 2

    Step 2: The value 10 is loaded from memory into a special location in the CPU called a register (e.g., Register A).

  3. 3

    Step 3: The value 5 is loaded into another register (e.g., Register B).

  4. 4

    Step 4: The CPU's arithmetic logic unit (ALU) is instructed to perform addition on the values in Register A and Register B.

  5. 5

    Step 5: The result of the addition (15) is stored in a register.

  6. 6

    Step 6: The result (15) is then stored in the memory location that the variable 'total' represents.

Recap

  • High-Level Languages (HLLs) are easy for humans to understand and are portable.
  • Low-Level Languages (LLLs) are close to machine code and are specific to a CPU.
  • One HLL instruction often translates into many LLL instructions.
  • HLLs use variable names (e.g., 'score'), while LLLs may use direct memory addresses.
  • Computers can only execute machine code (binary).

Quick check

  1. State one advantage of writing a program in a high-level language.1 mark
  2. Which type of language, HLL or LLL, is described as 'non-portable'?1 mark

2. Translators: Compilers and Interpreters

Since a CPU only understands machine code, any program written in a high-level language must be translated. This is done by a piece of software called a translator. There are two main types: compilers and interpreters. A compiler translates the entire program in one go, creating a separate executable file. This file can be run repeatedly without re-translation, making it fast. An interpreter translates and executes the code line by line. It doesn't create a separate executable file, and the translation happens every time the program is run.

Key term

Translator: A program that converts source code written in a programming language into machine code that the CPU can execute.

Examiner insight

Examiners reward answers that link the features of a translator to the specific needs of a given scenario (e.g., speed for a game, platform independence for a website).

Common pitfall

Stating that interpreters are 'slower' without explaining why. The key reason is that they re-translate the code every time it is run, especially within loops.

Worked example 13 marks

A company is developing a large commercial video game. Justify whether a compiler or an interpreter would be a more suitable translator for the final product.

  1. 1
    1. A compiler would be more suitable.
  2. 2
    1. Justification 1: A compiler translates the entire program into a standalone executable file. This means the game can be distributed to customers without them needing the translator or seeing the source code.
  3. 3
    1. Justification 2: Compiled programs run faster because the translation is done only once. For a performance-critical application like a video game, this speed is essential for smooth gameplay.
  4. 4
    1. An interpreter would be too slow as it translates line-by-line every time the game is played, which is not acceptable for a commercial product.

Recap

  • A compiler translates the entire source code at once into an executable file.
  • An interpreter translates and runs the source code one line at a time.
  • Compiled programs generally run faster than interpreted programs.
  • Interpreters report errors as soon as they are found, which is good for debugging.
  • Compilers report all errors at the end of the compilation process.
  • Interpreters are often used for web scripts as they are platform-independent.

Quick check

  1. Which translator creates a standalone executable file?1 mark
  2. Which translator is generally better for debugging and why?2 marks

3. Assemblers and Assembly Language

Assembly language is a type of low-level language that is a step above machine code. Instead of using binary 1s and 0s, it uses short, English-like mnemonics to represent each machine code instruction (e.g., `ADD` for addition, `MOV` for moving data). An assembler is the translator that converts assembly language code into the equivalent machine code. Each assembly language instruction generally corresponds to exactly one machine code instruction. This provides direct control over the CPU and memory, but the code is specific to a processor family and is not portable.

Key term

Assembler: A translator that converts low-level assembly language into machine code.

Worked example 13 marks

The following line of assembly code is written: `LDA #5`. This means 'LoaD the Accumulator with the value 5'. Explain why this is considered a low-level language instruction.

  1. 1
    1. It is a low-level language because it is very close to the hardware.
  2. 2
    1. The instruction refers to a specific part of the CPU, the 'Accumulator', which is a hardware register. High-level languages do not directly reference CPU registers.
  3. 3
    1. This single instruction will translate into a single machine code instruction, showing a one-to-one correspondence.
  4. 4
    1. The code is not easily readable without knowing the specific instruction set and is not portable to other CPU architectures.

Recap

  • Assembly language uses mnemonics to represent machine code instructions.
  • An assembler translates assembly language into machine code.
  • There is typically a one-to-one relationship between an assembly instruction and a machine code instruction.
  • Assembly language is processor-specific and therefore not portable.
  • It gives the programmer direct control over hardware components like CPU registers.

Quick check

  1. What is the name of the translator for assembly language?1 mark
  2. Give one reason why assembly language is not considered portable.1 mark

4. Data Types and 1D Arrays

When programming, we need to store data. A data type tells the computer what kind of data a variable can hold. Common data types include: Integer (whole numbers, e.g., 10, -5), Real/Float (numbers with decimal points, e.g., 9.99, 3.14), String (text, enclosed in quotes, e.g., "Hello"), Char (a single character, e.g., 'A'), and Boolean (can only be True or False). A one-dimensional array is a data structure that stores a collection of items of the same data type under a single name. Each item, or element, can be accessed using its index number, which usually starts from 0.

Key term

Array: A data structure containing a collection of elements of the same data type, each identified by an index.

Common pitfall

Forgetting that array indices start at 0, not 1. Accessing the 'third' item in an array `my_array` is done with `my_array[2]`.

Fun fact

The choice of data type can have a huge impact on memory usage. Storing the number 5 as a Real/Float can take up to four times more memory than storing it as a simple Integer.

Worked example 13 marks

A Python program contains the following lines: `name = "Alice"` `age = 16` `is_student = True` `scores = [88, 92, 79]`(a) Identify the data type of the variable `age`.(b) Write a line of Python to print the second score from the `scores` array.(c) What is the index of the value 79 in the `scores` array?

  1. 1

    (a) The data type of `age` is Integer, as it is a whole number.

  2. 2

    (b) `print(scores[1])`. We use index 1 because array indexing starts at 0, so the second element is at index 1.

  3. 3

    (c) The index of 79 is 2. (88 is at index 0, 92 is at index 1, 79 is at index 2).

Recap

  • An Integer is a whole number.
  • A Real (or Float) is a number with a decimal part.
  • A String is a sequence of characters, like text.
  • A Boolean can only be True or False.
  • An array is a list of items of the same type, accessed by an index.
  • Array indices almost always start from 0.

Quick check

  1. What data type would you use to store a person's phone number, e.g., "07700900123"? Explain your choice.2 marks
  2. An array `days` stores the 7 days of the week. What is the index of the first element?1 mark

5. Control Structures: Count-Controlled Iteration

Iteration means repeating a block of code. A count-controlled loop (often a 'for' loop) is a type of iteration that repeats a set number of times. It uses a counter variable that starts at a specified value, increments with each loop, and stops when it reaches an end value. This is perfect for when you know exactly how many times you need to perform an action, such as processing every item in an array or running a calculation 100 times.

Pseudocode: FOR counter = start_value TO end_value ... [code to repeat] ... NEXT counter

Python: for counter in range(start, stop): ... [code to repeat]

Key term

Iteration: The process of repeating a block of code, often managed by a loop.

Examiner insight

When asked to write an algorithm with a loop, ensure you clearly show the start and end points, the code that is being repeated, and how the loop variable is used.

Common pitfall

In many languages like Python, the `range(start, stop)` function stops *before* the `stop` number. `for i in range(1, 4)` will loop for i=1, 2, 3 but not 4.

Worked example 13 marks

Predict the output of the following pseudocode algorithm: `total = 0` `FOR i = 1 TO 4` ` total = total + i` `NEXT i` `OUTPUT total`

  1. 1

    Let's trace the value of `i` and `total` through the loop:

  2. 2

    Start: `total` is 0.

  3. 3

    Loop 1: `i` is 1. `total` becomes 0 + 1 = 1.

  4. 4

    Loop 2: `i` is 2. `total` becomes 1 + 2 = 3.

  5. 5

    Loop 3: `i` is 3. `total` becomes 3 + 3 = 6.

  6. 6

    Loop 4: `i` is 4. `total` becomes 6 + 4 = 10.

  7. 7

    The loop finishes. The final line outputs the value of `total`.

  8. 8

    Final Output: 10

Recap

  • Iteration is the repetition of a block of code.
  • A count-controlled loop ('for' loop) repeats a specific number of times.
  • These loops use a counter variable to keep track of the repetitions.
  • They are ideal for iterating through arrays or when the number of repetitions is known beforehand.
  • The loop stops when the counter variable reaches its defined end value.

Quick check

  1. Write a pseudocode loop that will output the numbers 5, 6, 7, and 8.2 marks

6. Program Errors and Debugging with Trace Tables

Even the best programmers make mistakes. Errors, or 'bugs', come in three main types. Syntax Errors are like grammatical mistakes in the programming language (e.g., spelling a command wrong, `prnt` instead of `print`). The translator will usually find these. Runtime Errors occur while the program is running (e.g., trying to divide by zero). Logic Errors are the trickiest; the program runs but produces the wrong output because the programmer's logic was flawed. A trace table is a key debugging tool to find logic errors. It's a table where you manually track the values of variables step-by-step through your code to see where the logic goes wrong.

Key term

Logic Error: An error in the design of an algorithm or program that causes it to produce an incorrect or unexpected result.

Examiner insight

When using a trace table, be methodical. Fill in one row for each step of the execution, even if a variable's value doesn't change, to show a complete trace.

Worked example 14 marks

The following algorithm is meant to calculate the sum of the numbers 3, 4, and 5. Use a trace table to find the logic error. Line 1: `total = 0` Line 2: `numbers = [3, 4, 5]` Line 3: `FOR i = 0 TO 2` Line 4: ` total = numbers[i]` Line 5: `NEXT i` Line 6: `OUTPUT total`

  1. 1

    We will create a trace table with columns for the Line number, `i`, and `total`.

  2. 2

    | Line | i | total | Comment |

  3. 3
  4. 4

    | 1 | - | 0 | `total` is initialised. |

  5. 5

    | 2 | - | 0 | Array is defined. |

  6. 6

    | 3 | 0 | 0 | Loop starts, `i` is 0. |

  7. 7

    | 4 | 0 | 3 | `total` becomes `numbers[0]`, which is 3. |

  8. 8

    | 3 | 1 | 3 | Next loop, `i` is 1. |

  9. 9

    | 4 | 1 | 4 | `total` becomes `numbers[1]`, which is 4. |

  10. 10

    | 3 | 2 | 4 | Next loop, `i` is 2. |

  11. 11

    | 4 | 2 | 5 | `total` becomes `numbers[2]`, which is 5. |

  12. 12

    | 5 | 2 | 5 | Loop ends. |

  13. 13

    | 6 | - | 5 | Output is 5. |

  14. 14

    The expected output is 3+4+5=12, but the actual output is 5. The error is on Line 4. It should be `total = total + numbers[i]` to accumulate the sum, not just overwrite the total each time.

Recap

  • A syntax error is a mistake in the language's grammar.
  • A runtime error happens during program execution.
  • A logic error means the program runs but gives the wrong result.
  • A trace table is used to manually track variable values to find logic errors.
  • Debugging is the process of finding and fixing errors in a program.

Quick check

  1. A program crashes when a user enters text instead of a number. What type of error is this?1 mark
  2. What is the primary purpose of a trace table?1 mark

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. Explain two differences between a high-level language and a low-level language.4 marks
  2. A web developer is creating an interactive script for a website that needs to run on various browsers and operating systems (Windows, macOS, Android). Explain why an interpreter is a more suitable choice of translator than a compiler.3 marks
  3. Identify the most appropriate data type (Integer, Real, String, Boolean) for storing each of the following pieces of data: (a) The number of students in a class. (b) A person's postcode. (c) The price of an item. (d) Whether a light is switched on or off.4 marks
  4. Differentiate between a syntax error and a logic error. For each, provide a simple one-line code example that would cause the error.4 marks
  5. An array, `shopping_list`, is defined as `["Apples", "Bread", "Milk"]`. Write a pseudocode algorithm that uses a count-controlled loop to output each item from the list on a new line.4 marks
  6. What is the role of an assembler, and why is assembly language considered 'processor-specific'?3 marks
  7. Predict the final output of the following Python program and show your working. `count = 0 for x in range(1, 11): if x % 3 == 0: count = count + 1 print(count)`3 marks
  8. A program is written to find the largest number in a list. It contains a logic error. Complete a trace table for the algorithm below to find the error. Algorithm: Line 1: `list = [10, 50, 20]` Line 2: `max_num = 0` Line 3: `FOR i = 0 TO 2` Line 4: ` IF list[i] > max_num THEN` Line 5: ` max_num = list[0]` Line 6: ` END IF` Line 7: `NEXT i` Line 8: `OUTPUT max_num`5 marks
  9. Compare the process of program translation for a compiler with that of an interpreter, from the moment the programmer finishes writing the code to the point where the CPU executes instructions.5 marks
  10. A program needs to store the names of 100 runners. Explain why using an array is a more efficient approach than declaring 100 separate string variables.2 marks

Go deeper

Practise and revise with member-only material for this chapter.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters