2.2.1 Programming Fundamentals
In this lesson, you will learn the fundamental concepts used to create computer programs. You will explore variables, constants, assignments, inputs, outputs, and operators. You will also use and recognise these concepts in OCR Exam Reference Language and Python.
Programming
Programming involves giving a computer a precise set of instructions to follow. Programs need ways to store data, process it, make decisions, and repeat instructions. These ideas form the foundations of programming and are used in programs of every size.
In OCR GCSE Computer Science, programming questions may be presented using the OCR Exam Reference Language (ERL). This is a standardised notation that allows programming concepts to be shown without relying on one particular programming language. Throughout this course, Python is used as the main example high-level programming language.
Variables, Constants, and Identifiers
Programs often need to store data while they are running. Variables and constants provide named locations where this data can be stored.
A variable stores a value that can change while a program is running. Each variable has an identifier and a data type, such as integer, real/float, or string. Data types are covered in Lesson 2.2.3 Data Types. For example, a weather program might store the current temperature in a variable called . Its value could change whenever a new reading is received.
A constant stores a value that should remain fixed throughout the program. It is assigned a value once and cannot be changed during runtime. The same program might use a constant called with a fixed value of 0.
The name given to a variable or constant is called an identifier. Identifiers should clearly describe the data they hold. Programming languages have rules about which identifiers are allowed. For example, identifiers generally cannot contain spaces, begin with a number, or use a reserved keyword that already has a special meaning in the language.
| Examples of Suitable Identifiers | Examples of Unsuitable Identifiers |
|---|---|
| (contains a space) | |
| (begins with a number) | |
| (reserved keyword in Python) |
Most programming languages, including Python, are case-sensitive, meaning that , , and are treated as different identifiers.
| Concept | OCR ERL | Python |
|---|---|---|
| Variable | ||
| Constant |
Python does not have a special keyword that prevents a value from being changed. Instead, programmers conventionally write constant identifiers in capital letters to indicate that the value should remain unchanged.
Variables and constants provide the places where data can be stored, but a program also needs a way to place values into them. This is done using assignment. An assignment stores a value in a variable or constant. In both the OCR Exam Reference Language and Python, the symbol is used for assignment.
The identifier appears on the left-hand side, while the value or expression being assigned appears on the right-hand side. For example, assigns the value 1 to . A variable can be assigned a new value later, as shown in Code 1 below.
attempts = 1
attempts = 2
attempts = attempts + 1The first instruction assigns the value 1 to . The second instruction replaces this with 2, so the old value is lost. The final instruction first reads the current value of , adds 1, and assigns the result back to the same variable. The final value of is therefore 3.
Assignment does not mean that the two sides of = are mathematically equal. The expression on the right-hand side is evaluated first, and the resulting value is then stored in the identifier on the left.
In , the current value of attempts is read first, 1 is added, and the new result is then assigned back to . This is a common way of increasing a variable.
Inputs and Outputs
Programs need to receive data and communicate results. This is accomplished with inputs and outputs.
An input is data supplied to a program. It might come from a user, a sensor, a file, or another system. An output is data produced by a program. This could be displayed on a screen, written to a file, printed, or sent to another device.
In the OCR Exam Reference Language, can be used to receive user input and to produce an output, as shown in Code 2 below.
name = input("Enter your name")
print("Hello", name)The first instruction asks the user for their name and stores the response in . The second instruction outputs a greeting containing the stored value, by combining the string “Hello” and the variable . Python uses the same and function names as shown in Code 3 below.
name = input("Enter your name: ")
print("Hello", name)receives the value entered by the user, and the assignment stores that value in . The following instruction then reads the stored value and includes it in the output.
Operators
An operator is a symbol or keyword that tells the program to perform an operation on one or more values. The three main groups of operators are arithmetic operators (which perform calculations), comparison operators (which compare values), and Boolean operators (which combine or reverse conditions).
Arithmetic Operators
Arithmetic operators allow programs to perform mathematical calculations. These operators are listed in Table 3 below.
| Operator | OCR ERL | Python | Purpose |
|---|---|---|---|
| Addition | Adds two values. | ||
| Subtraction | Subtracts the second value from the first. | ||
| Multiplication | Multiplies two values. | ||
| Division | Divides the first value by the second. | ||
| Modulus | \texttt{%} | Returns the remainder after division. | |
| Quotient | Returns the whole-number quotient, ignoring the remainder. | ||
| Exponentiation | Raises one value to the power of another. |
For example, because 17 divided by 5 leaves a remainder of 2. By comparison, because 5 fits into 17 three complete times and the remainder is ignored.
The modulus operator is particularly useful for checking whether a number is odd or even. If , then n is even because dividing it by 2 leaves no remainder.
When an expression contains several arithmetic operators, the computer does not simply work from left to right. Instead, it follows the same basic precedence (order of operation) rules used in mathematics (BIDMAS, BODMAS, or equivalent).
Brackets are evaluated first, followed by powers, then multiplication and division, then addition and subtraction. Multiplication and division have equal precedence and are evaluated from left to right; addition and subtraction work in the same way. Brackets can be used both to change the order of evaluation and to make the programmer’s intention clearer.
For example, is evaluated as , and then . Changing the order of operations could produce a different result, so brackets can be used to make the intended calculation clear.
Comparison Operators
A comparison operator compares two values and produces a Boolean result: either True or False. These operators are listed in Table 4 below.
| Operator | OCR ERL | Python | Purpose |
|---|---|---|---|
| Equal to | Checks if two values are equal. | ||
| Not equal to | Checks if two values are not equal. | ||
| Less than | Checks if the value on the left is less than the value on the right. | ||
| Less than or equal to | Checks if the value on the left is less than or equal to the value on the right. | ||
| Greater than | Checks if the value on the left is greater than the value on the right. | ||
| Greater than or equal to | Checks if the value on the left is greater than or equal to the value on the right. |
For example, evaluates to True if age contains 18 or a greater value, and False otherwise. Comparison operators do not change or store a value. They evaluate a condition and produce either True or False.
Boolean Operators
A Boolean condition is an expression that can evaluate to only True or False. Boolean operators allow several conditions to be combined or reversed. The three Boolean operators are AND, OR, and NOT, as listed in Table 5 below.
| Operator | OCR ERL | Python | Purpose |
|---|---|---|---|
| AND | True only when both conditions are true. | ||
| OR | True when at least one condition is true. | ||
| NOT | Reverses a Boolean result. |
With AND, every condition must be true. If even one condition is false, the overall result is false.
With OR, at least one condition must be true. The overall condition is also true if both conditions are true.
NOT applies to a Boolean value or condition and reverses its result: True becomes False, and False becomes True.
These relationships are summarised in the Boolean-outcome tables Table 6 and Table 7 below. These concepts are explored further in Module 2.4 Boolean Logic.
| A | B | A AND B | A OR B |
|---|---|---|---|
| False | False | False | False |
| False | True | False | True |
| True | False | False | True |
| True | True | True | True |
| A | NOT A |
|---|---|
| False | True |
| True | False |
Suppose a program should allow access only when both a username and password are correct, as shown in Code 4 below.
if username == "admin" AND password == "secure123" then
print("Access granted")
else
print("Access denied")
endifThe operator means that both conditions must be true for "Access granted" to be output.
As shown in Code 5 below, the operator is different.
if day == "Saturday" OR day == "Sunday" then
print("Weekend")
endifOnly one of the conditions needs to be true, although the overall result is also True if both conditions are true.
The operator reverses a Boolean result as shown in Code 6 below.
if NOT(loggedIn) then
print("Please log in")
endifIf loggedIn is False, applying makes the condition True. Alternatively, if loggedIn is True, applying makes the condition False.
