Cambridge O Level2210

Programming concepts

Computer Science 2210 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.

~8 min read

1. Introduction to Programming

Programming is the process of writing a set of instructions, called a program, that a computer can execute to perform a specific task. These instructions are written in a programming language. We can think of a program as the implementation of an algorithm, which is a step-by-step plan for solving a problem. Languages like Python, Java, and VB.NET are high-level languages because they are closer to human language and abstract away complex computer operations. For designing and planning, we use pseudocode. Pseudocode is a structured, text-based description of an algorithm that is not tied to any specific programming language, making it a universal tool for developers.

Key term

Pseudocode: A plain language description of the steps in an algorithm, intended for human reading rather than machine execution.

Examiner insight

Examiners expect you to write clear, unambiguous pseudocode that follows standard conventions. Logic is more important than exact syntax.

Common pitfall

Writing pseudocode that is too similar to a specific programming language, which defeats its purpose as a language-independent planning tool.

Fun fact

The first recognised programmer was Ada Lovelace, who in the 1840s wrote the first algorithm intended to be processed by Charles Babbage's proposed mechanical computer, the Analytical Engine.

Worked example 13 marks

Write a simple algorithm in pseudocode to ask for a user's name and then display a personalised greeting.

  1. 1
    1. Display a prompt to the user: OUTPUT "What is your name?"
  2. 2
    1. Read the user's input and store it: INPUT UserName
  3. 3
    1. Concatenate the greeting with the user's name and display it: OUTPUT "Hello, " + UserName

Recap

  • Programming is the act of writing instructions for a computer.
  • An algorithm is a logical, step-by-step procedure for solving a problem.
  • High-level languages are human-readable and need to be translated for a computer to understand.
  • Pseudocode is an essential tool for planning a program's logic before writing any code.

Quick check

  1. What is the relationship between an algorithm and a program?2 marks
  2. Why would a programmer use pseudocode before writing in Python or Java?1 mark

2. Variables: Storing Changing Data

A variable is a named location in a computer's memory used to store data. The key feature of a variable is that the data it holds can be changed while the program is running. Think of it as a labelled box where you can store a piece of information, look at it, and replace it with new information later. Before using a variable, you must 'declare' it, which means giving it a name (its identifier) and specifying the type of data it will hold. The process of putting a value into a variable is called 'assignment'.

Declaration: DECLARE <identifier> : <data_type>

Assignment: <identifier> ← <value>

Key term

Variable: A named memory location that stores a value which may change during the execution of a program.

Examiner insight

Marks are consistently awarded for correctly declaring variables with appropriate data types and using them to store and update values as the program logic dictates.

Common pitfall

Forgetting to initialise a variable before using it in a calculation, especially for totals or counters, which can lead to errors.

Worked example 13 marks

A program needs to keep track of a player's score in a game. Write pseudocode to:(a) Declare a variable for the score.(b) Set the initial score to 0.(c) Add 10 points to the score.

  1. 1
    1. (a) Declare an integer variable named PlayerScore: DECLARE PlayerScore : INTEGER
  2. 2
    1. (b) Assign the initial value: PlayerScore ← 0
  3. 3
    1. (c) Update the score. The new value is the old value plus 10: PlayerScore ← PlayerScore + 10
  4. 4
    1. (Optional) Display the new score to verify: OUTPUT PlayerScore

Recap

  • A variable is a named location in memory for storing data.
  • The value of a variable can be changed at any time during program execution.
  • Variables must be declared before use, specifying a name and data type.
  • The assignment operator (← in pseudocode) is used to store a value in a variable.

Quick check

  1. What is the purpose of declaring a variable?1 mark
  2. Write the pseudocode to assign the value 25 to a variable named 'age'.1 mark

3. Constants: Storing Fixed Values

A constant is a named memory location that stores a value that cannot be changed after it has been defined. It is set once at the beginning of the program and remains fixed throughout execution. Constants are used for values that are fundamental to the program's logic and should not be altered, such as a mathematical value (e.g., PI), a physics constant (e.g., speed of light), or a business rule (e.g., VAT_RATE). Using constants makes code more readable and much easier to maintain; if the value needs to change, you only have to update it in one place.

Declaration: CONSTANT <identifier> = <value>

Key term

Constant: A named memory location that stores a value which cannot be changed during the execution of a program.

Examiner insight

Examiners look for the use of constants as evidence of good programming practice and an understanding of code maintainability.

Common pitfall

Hard-coding a fixed value (like 0.20 for tax) multiple times in a program instead of defining it once as a constant.

Worked example 14 marks

A program calculates the area of a circle. The value of Pi will be used.(a) Declare a suitable constant for Pi.(b) Write the pseudocode to calculate the area for a given radius of 5 units.

  1. 1
    1. (a) Declare a constant for Pi: CONSTANT PI = 3.142
  2. 2
    1. (b) Declare variables for radius and area: DECLARE Radius, Area : REAL
  3. 3
    1. Assign the given radius: Radius ← 5.0
  4. 4
    1. Calculate the area using the formula A = πr²: Area ← PI * Radius * Radius
  5. 5
    1. Output the result: OUTPUT "The area is ", Area

Recap

  • A constant stores a value that cannot be changed while the program is running.
  • Constants are declared and assigned a value once.
  • Using constants for fixed values makes programs easier to read and maintain.
  • If a fixed value needs updating, changing the constant declaration is all that is required.

Quick check

  1. State one key difference between a variable and a constant.1 mark
  2. Why is it good practice to use a constant for a value like a tax rate?2 marks

4. Using Meaningful Identifiers

An identifier is the name given to a program element, such as a variable or a constant. Choosing meaningful, descriptive identifiers is one of the most important aspects of writing good code. A name like 'studentFirstName' immediately tells you what it stores, whereas a name like 's' or 'x1' is meaningless without extra comments. Good identifiers make code self-documenting, easier to debug, and simpler for others (and your future self) to understand. While rules vary slightly between languages, identifiers typically must start with a letter, cannot contain spaces, and are case-sensitive.

Key term

Identifier: A name given by a programmer to a variable, constant, or other program element.

Examiner insight

In questions that require you to write code, using meaningful identifiers is a component of a high-quality answer and can contribute to marks for clarity and maintainability.

Common pitfall

Using identifiers that are too short and cryptic (e.g., 'n') or too long and cumbersome (e.g., 'theTotalNumberOfStudentsInTheClass').

Worked example 13 marks

A programmer has written code to calculate an employee's weekly wage. Rewrite it using meaningful identifiers. `h = 40`, `r = 15.50`, `p = h * r`, `OUTPUT p`

  1. 1
    1. Replace 'h' with a descriptive name: DECLARE HoursWorked : INTEGER
  2. 2
    1. Replace 'r' with a descriptive name: DECLARE HourlyRate : REAL
  3. 3
    1. Replace 'p' with a descriptive name: DECLARE PayAmount : REAL
  4. 4
    1. Assign values using the new identifiers: HoursWorked ← 40
  5. 5
    1. HourlyRate ← 15.50
  6. 6
    1. Perform the calculation: PayAmount ← HoursWorked * HourlyRate
  7. 7
    1. Output the result with context: OUTPUT "Total pay is: £", PayAmount

Recap

  • Identifiers are the names you give to variables, constants, and other program elements.
  • Always use meaningful identifiers that clearly describe their purpose.
  • Good naming makes code easier to read, debug, and maintain.
  • Avoid single-letter or cryptic identifiers like 'x' or 'temp_val_1'.

Quick check

  1. Which is a better identifier for a user's age: 'a' or 'userAge'? Explain why.2 marks
  2. State one common rule for creating a valid identifier.1 mark

End-of-chapter exercise

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

  1. Explain the difference between a variable and a constant, giving an example use case for each.4 marks
  2. What is pseudocode and why is it used in the program development process?2 marks
  3. A program needs to store the name of a student, their test score (out of 100), and whether they passed the test (pass mark is 50). Declare appropriate variables for each, using meaningful identifiers and assigning suitable data types.3 marks
  4. The following pseudocode is intended to calculate the circumference of a circle given its radius. It contains several errors and examples of poor practice. Rewrite the pseudocode correctly, using meaningful identifiers and a constant where appropriate. `r = 5` `pi = 3.14` `OUTPUT r * 2 * pi`4 marks
  5. Write a pseudocode algorithm that asks for the user's first name and last name, stores them in two separate variables, and then outputs a welcome message that includes their full name.4 marks
  6. What is an identifier? State two rules that generally apply to identifiers in high-level programming languages.3 marks
  7. A shop offers a 10% discount on all items. Write a pseudocode snippet that: a) Declares a constant for the discount rate. b) Declares variables for the original price and final price. c) Assigns a value of 75.50 to the original price. d) Calculates the final price after the discount. e) Outputs the final price.5 marks
  8. Why is `MaxLoginAttempts` a better identifier than `mla`?2 marks
  9. Write a pseudocode algorithm to calculate and output the number of seconds in a given number of days. The program should first ask the user to input the number of days. Use constants for the number of hours in a day, minutes in an hour, and seconds in a minute.6 marks
  10. Explain why using a constant for a value like a tax rate makes a program more maintainable.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