Exam code:J277
Taking an algorithm and turning it into code, in any language, requires an understanding of several basic programming concepts such as:
-
Variables
-
Constants
-
Assignment
-
Operators
-
Inputs
-
Outputs
Variables, Constants & Assignments
What is a variable?
-
A variable is a named memory location that holds data that during the execution of a program, the data can change
-
Variables can store a variety of different types of data such as numbers, text or true/false values
-
To store data in a variable, the process of assignment is used
What is a constant?
-
A constant is fixed data that during the execution of a program cannot change
-
A constant can store a variety of different types of data, similar to variables
-
Pi is an example of a mathematical fixed value that would typically be stored as a constant
What is assignment?
-
Assignment is the process of storing data in a variable or constant under a descriptive name
-
Assignment is performed using the ‘=‘ symbol
Assigning variables & constants
|
Concept |
OCR exam reference |
Python |
|---|---|---|
|
Variables |
|
|
|
Constants |
|
|
Operators, Inputs & Outputs
What is an operator?
-
An operator is a symbol used to instruct a computer to perform a specific operation on one or more values
-
Examples of common operators include:
-
Arithmetic
-
Comparison
-
Boolean (AND, OR and NOT)
-
Arithmetic
|
Operator |
OCR exam reference |
Python |
|---|---|---|
|
Addition |
|
|
|
Subtraction |
|
|
|
Multiplication |
|
|
|
Division |
|
|
|
Modulus (remainder after division) |
|
|
|
Quotient (whole number division) |
|
|
|
Exponentiation (to the power of) |
|
|
Comparison
|
Operator |
OCR exam reference |
Python |
|---|---|---|
|
Equal to |
|
|
|
Not equal to |
|
|
|
Less than |
|
|
|
Less than or equal to |
|
|
|
Greater than |
|
|
|
Greater than or equal to |
|
|
Examples
|
Operator |
OCR exam reference |
Python |
|---|---|---|
|
Addition |
|
|
|
Multiplication |
|
|
|
Modulus |
|
|
|
Quotient |
|
|
|
Exponentiation |
|
|
|
Equal to |
|
|
|
Not equal to |
|
|
|
Greater than or equal to |
|
|
|
AND |
|
|
What is an input?
-
An input is a value that is read from an input device and then processed by a computer program
-
Typical input devices include:
-
Keyboards – Typing text
-
Mice – Selecting item, clicking buttons
-
Sensors – Reading data from sensors such as temperature, pressure or motion
-
Microphone – Capturing audio, speech recognition
-
-
Without inputs, programs are not useful as they can’t interact with the outside world and always produce the same result
What is an output?
-
An output is a value sent to an output device from a computer program
-
Typical output devices include:
-
Monitor – Displaying text, images or graphics
-
Speaker – Playing audio
-
Printer – Creating physical copies of documents or images
-
Area of a rectangle program
|
OCR exam reference |
|---|
|
|
Python |
|
Worked Example
A cinema calculates ticket prices based on age category
-
Adult = £13.00
-
Child = £7.50
The program asks the user to enter their age and calculates the cost of their ticket
A simple algorithm is used
adult = 13.00
child = 7.50
age = input("What is your age: ")
if age > 18 then total_cost = adult
else total_cost = child
end if
print(total_cost)
The cinema decides to add a discount of 25% to customers who come to the cinema on ‘Sunday evening’
Identify all the additional inputs that will be required for this change to the algorithm [2]
How to answer this question
-
What new information is needed?
Answer
-
day
-
time
Responses