Cambridge IGCSE0478

Programming concepts

Computer Science 0478 Chapter Notes

What this chapter covers

Programming conceptsArraysFile handling
ShareWhatsAppPost
Programming concepts 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 concepts notes as text: skim, search, and jump between subtopics.

~10 min read

1. Understanding Programs and Languages

A computer program is a sequence of instructions that a computer follows to perform a specific task. These instructions are written by programmers using a programming language. There are two main types of programming languages: high-level and low-level. High-level languages, like Python or Java, use English-like commands and are easier for humans to read, write, and debug. They are 'portable', meaning they can run on different types of computers with little or no modification. Low-level languages, like assembly language, are much closer to the computer's native language (machine code) and give the programmer very direct control over the computer's hardware. Machine code itself is the lowest level, consisting of binary digits (1s and 0s) that the CPU can execute directly.

Key term

High-Level Language: A programming language with strong abstraction from the computer's internal architecture, which is easy for humans to read and write.

Examiner insight

Examiners expect you to know that high-level languages are portable and easier to debug, while low-level languages offer more direct control over hardware and can be faster.

Common pitfall

Confusing assembly language with machine code. Assembly language uses text-based mnemonics and is a low-level language, but it still needs to be translated by an assembler into machine code (binary) for the CPU to execute.

Fun fact

The first high-level programming language was called 'Plankalkül', designed by Konrad Zuse in Germany in the 1940s for his Z3 computer. However, it wasn't implemented until decades later.

Worked example 14 marks

Compare two characteristics of high-level languages and low-level languages. [4]

  1. 1

    Characteristic 1: Readability. High-level languages use English-like statements (e.g., 'PRINT name'), making them easy for humans to read and understand. Low-level languages use mnemonics (e.g., 'LDA #10') or binary, which is difficult to read.

  2. 2

    Characteristic 2: Portability. High-level language programs are generally portable and can run on different computer systems with minimal changes. Low-level language programs are often specific to a particular CPU architecture and are not portable.

  3. 3

    Characteristic 3: Hardware Control. Low-level languages provide direct control over the system hardware and memory, which is useful for creating device drivers or operating systems. High-level languages abstract away the hardware, making this level of control difficult.

  4. 4

    Characteristic 4: Debugging. It is easier to find and fix errors (debug) in high-level languages because the code is more understandable and they have better built-in tools. Debugging low-level code is a more complex and time-consuming process.

Recap

  • A program is a set of instructions for a computer to execute.
  • High-level languages are human-friendly, portable, and easier to debug.
  • Low-level languages are machine-specific and offer direct hardware control.
  • Assembly language is a low-level language that uses mnemonics.
  • Machine code is binary data that the CPU executes directly.

Quick check

  1. State two advantages of writing a program in a high-level language.2 marks

2. Code Translators: Compilers and Interpreters

Since a computer's CPU can only understand machine code, any program written in a high-level or low-level language must be translated. This is done by a piece of software called a translator. The two main types of translators for high-level languages are compilers and interpreters. A compiler reads the entire program (source code) and translates it all at once into a complete machine code program (an executable file). This executable can then be run at any time without the compiler. An interpreter, on the other hand, reads the source code one line at a time, translates that line into machine code, executes it, and then moves to the next line. It does this every time the program is run.

Key term

Compiler: A program that translates source code from a high-level programming language into machine code all at once to create a standalone executable file.

Examiner insight

Marks are often awarded for clearly distinguishing the output of each translator: a compiler produces a separate executable file which can be run independently, while an interpreter executes the code directly, line by line.

Common pitfall

Thinking that interpreters are 'worse' than compilers. Both are tools with different strengths. Interpreters are excellent for learning and rapid development, while compilers are better for performance and distribution.

Worked example 13 marks

A software company is about to release a new application. Explain why they would use a compiler, rather than an interpreter, for the final version given to customers. [3]

  1. 1
    1. Speed of Execution: The compiled program will run faster because it has already been translated into machine code. An interpreter translates line-by-line each time it runs, which is slower.
  2. 2
    1. No Translator Needed: The customer only needs the executable file to run the program. They do not need to have the compiler or a copy of the programming environment installed.
  3. 3
    1. Source Code Protection: The company gives the customer the executable machine code, not the original source code. This protects their intellectual property as machine code is extremely difficult to reverse-engineer.

Worked example 22 marks

During the development and testing of a program, a programmer might prefer to use an interpreter. Explain one reason why. [2]

  1. 1

    Reason: Debugging is easier. An interpreter runs code line-by-line and stops at the first error it finds.

  2. 2

    Explanation: This immediately directs the programmer to the exact location of the problem, making it faster to find and fix errors during development compared to a compiler which may report many errors at once after compiling the whole program.

Recap

  • High-level code must be translated into machine code to be run by the CPU.
  • A compiler translates the entire source code at once, creating an executable file.
  • An interpreter translates and executes the source code line-by-line.
  • Compiled programs run faster and do not require the translator to be present.
  • Interpreters are often better for debugging as they pinpoint errors immediately.

Quick check

  1. Which type of translator produces an executable file?1 mark
  2. Which type of translator stops as soon as it finds the first error?1 mark

3. Using Variables and Constants

When a program runs, it needs to store data temporarily in the computer's memory (RAM). We use variables and constants to do this. A variable is a named memory location for storing a piece of data that may change while the program is running. For example, a variable called 'score' could start at 0 and increase as a player does well in a game. A constant is a named memory location for a value that does not change. For example, you could store the value of Pi (3.142) or the number of hours in a day (24) as a constant. Using constants makes code more readable and easier to update; if a value needs to change, you only have to change it in one place. In pseudocode, we use specific keywords to create them.

DECLARE <identifier> : <data_type>

CONSTANT <identifier> = <value>

Key term

Variable: A named memory location that stores a value which can change while the program is running.

Examiner insight

Examiners look for correct use of the pseudocode keywords 'DECLARE' and 'CONSTANT', as well as a sensible choice of data type for the value being stored.

Common pitfall

Forgetting that a constant's value is fixed when it is declared and cannot be changed later in the program. Attempting to assign a new value to a constant will cause an error.

Worked example 13 marks

A program is needed to calculate the area of a circle (Area = πr²). The user will enter the radius. Write pseudocode to declare a constant for Pi and a variable for the radius. Use suitable data types. [3]

  1. 1
    1. Identify the constant: Pi is a fixed value, so it should be a constant. Its value has a decimal point, so its data type should be REAL.
  2. 2

    Pseudocode for constant: CONSTANT Pi = 3.142

  3. 3
    1. Identify the variable: The radius will be entered by the user and could be any number, so it's a variable. It could be a whole number or have a decimal, so REAL is a safe data type.
  4. 4

    Pseudocode for variable: DECLARE Radius : REAL

  5. 5
    1. Final Answer combining both declarations: CONSTANT Pi = 3.142, DECLARE Radius : REAL

Recap

  • A variable is a named memory location for data that can change.
  • A constant is a named memory location for data that cannot change.
  • Variables are declared using 'DECLARE identifier : datatype'.
  • Constants are declared using 'CONSTANT identifier = value'.
  • Using constants makes code easier to read and maintain.

Quick check

  1. Write the pseudocode to declare a variable called 'Age' to store a whole number.1 mark
  2. Should you use a variable or a constant to store the number of minutes in an hour? Why?2 marks

4. Writing Readable Code: Meaningful Identifiers

An identifier is the name a programmer gives to a variable, constant, subroutine, or other part of a program. Choosing good identifiers is one of the most important habits for writing high-quality code. Instead of using short, cryptic names like 'x', 'y', or 'n', you should use meaningful identifiers that describe the data they hold. For example, instead of `DECLARE n : STRING`, use `DECLARE StudentName : STRING`. This makes your code 'self-documenting'—it's much easier for you (and others) to read, understand, and debug your code later. Good identifiers make your program more maintainable. While `studentName` and `student_name` are both good, be consistent with the style you choose.

Key term

Identifier: A name given by the programmer to a variable, constant, or subroutine in a program.

Examiner insight

In programming tasks, using meaningful identifiers can contribute to marks for code clarity and maintainability, even if not explicitly asked for. It demonstrates good programming practice.

Common pitfall

Using identifiers that are also keywords in the programming language (e.g., naming a variable 'print' or 'if'). This will confuse the translator and cause a syntax error.

Worked example 13 marks

A student has written the following line of code to store the price of an item in a shop: `DECLARE x : REAL`. Explain two reasons why 'x' is a poor choice for an identifier and suggest a better alternative. [3]

  1. 1

    Reason 1 (Readability): The identifier 'x' gives no clue as to what data it is storing. This makes the code difficult to understand without reading comments or surrounding code.

  2. 2

    Reason 2 (Maintainability): If another programmer (or the original programmer a few months later) needs to modify the code, they will have to spend extra time figuring out the purpose of 'x'. This slows down debugging and maintenance.

  3. 3

    Suggested Alternative: A much better identifier would be `ItemPrice` or `priceOfItem`. This immediately tells anyone reading the code that the variable is used to store the price of an item.

Recap

  • An identifier is a name for a variable, constant, or other program element.
  • Use meaningful identifiers to make code readable and self-documenting.
  • Good identifiers make programs easier to debug and maintain.
  • Avoid single-letter identifiers (except for simple loop counters like 'i').
  • Do not use programming language keywords (like 'IF' or 'PRINT') as identifiers.

Quick check

  1. A variable is used to store the number of lives a player has left. Suggest a suitable identifier.1 mark
  2. Why is it a bad idea to name a variable 'FOR'?1 mark

End-of-chapter exercise

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

  1. Define the term 'program'.1 mark
  2. State two differences between a variable and a constant.2 marks
  3. Explain why a program written in a high-level language must be translated before a computer can run it.2 marks
  4. A programmer is developing a new game. They choose to use a compiler instead of an interpreter. Describe two benefits of using a compiler for the final released version of the game.4 marks
  5. A school needs a program to store student data. Write pseudocode to declare the following: A variable to store a student's first name, a variable to store their exam score as a whole number, and a constant to store the school's passing mark of 40.3 marks
  6. Explain, using an example, why it is important to use meaningful identifiers when writing a program.3 marks
  7. Compare and contrast the use of a compiler and an interpreter during the development process of a large software application. Your answer should consider speed of testing, debugging, and final execution speed.6 marks
  8. A program is written with the following variable declarations: DECLARE x : STRING, DECLARE y : INTEGER. The variables are intended to store a player's username and their highest score. (a) Criticise the choice of identifiers. (b) Rewrite the declarations using more appropriate identifiers.4 marks
  9. Describe the relationship between assembly language and machine code.2 marks
  10. Give one example of a high-level programming language and one characteristic of a low-level programming language.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