MadeByZack
Sign in
Back

2.2.1 Programming fundamentals · Paper 2

Programming basics: variables, if and loops

Each section has pictures and things to try, then its key points and key words. Exam practice is at the end of the page.

You need to know

  • Variables, constants, operators, inputs, outputs and assignment
  • The three constructs: sequence, selection and iteration (count-controlled and condition-controlled loops)
  • Arithmetic operators, including MOD, DIV and ^
  • Comparison operators, and the Boolean operators AND, OR and NOT
  • Using all of these in a real programming language in class

You don't need to know

  • OCR doesn't rule anything out here.

The building blocks

VariableA named store for a value that can change while the program runs.
ConstantA named value that does not change, e.g. VAT = 0.2.
AssignmentGiving a variable a value, e.g. score = 0.
Input / outputGetting data from the user, and showing results.

Try it

Variable or constant?

  • VAT = 0.2, the tax rate used in a shop
  • score, which goes up each time you win a point
  • PI = 3.14159
  • name, typed in by the user
  • MAX_PLAYERS = 4
  • lives, which goes down when you are hit

Key points

Variables and constants

  • A variable is a named place in memory that stores a value. The value can change while the program runs.
  • A constant is a named value that can't change while the program runs, e.g. the VAT rate.
  • Constants mean you only change the value in one place, and it can't be changed by mistake.
  • In the OCR reference language you write const VAT = 0.2. In Python, constants are written in capitals by habit.
  • Use names that say what they store, like score, not x.

Assignment

Before this: Variables and constants

  • Assignment means giving a variable a value, using =.
  • The right-hand side is worked out first, then stored in the variable on the left.
  • score = score + 1 means take the score, add 1, and store it back in score.
  • Don't mix up = (assignment) with == (checking if two things are equal).

Input and output

Before this: Variables and constants

  • Input gets data from the user: name = input("Name?").
  • Input always arrives as a string, so cast it if you need a number.
  • Output shows data to the user: print("Hello").
  • You can join text and variables in an output with +.

Key words

Say what each one means, then tap to check.

The three constructs

SequenceInstructions run one after another, in order.
SelectionA decision: if / elif / else. Different code runs depending on a condition.
IterationRepeating code. Count-controlled (for) runs a set number of times. Condition-controlled (while) runs until a condition changes.
age = int(input("Age? "))       # input + assignment
if age >= 13:                    # selection
    print("You can join")
for i in range(3):               # count-controlled loop
    print("Hello")
while age < 0:                   # condition-controlled loop
    age = int(input("Try again: "))

Try it

Which construct is each one?

  • for i in range(10):
  • if age >= 18:
  • while lives > 0:
  • Three lines that ask for a name, ask for an age, then print both
  • Keep asking for the password until it is right
  • Print the 12 times table, from 1 × 12 to 12 × 12
  • elif score > 50:

Key points

Sequence

Before this: Input and output

  • Sequence means instructions run one after another, in order.
  • It is one of the three programming constructs, with selection and iteration.
  • The order matters. A value must be input or worked out before it is used.

Selection (if)

Before this: Comparison operators

  • Selection means the program makes a decision and runs different code depending on a condition.
  • if checks a condition. elseif (Python: elif) checks another. else runs if none were true.
  • Only the first true branch runs, so put the conditions in a sensible order.
  • The OCR reference language also has switch / case for checking one variable against many values.

Python

if score >= 50:
    print("Pass")
elif score >= 40:
    print("Close")
else:
    print("Fail")

OCR reference language

if score >= 50 then
    print("Pass")
elseif score >= 40 then
    print("Close")
else
    print("Fail")
endif

Count-controlled loops (for)

Before this: Sequence

  • A count-controlled loop repeats a set number of times.
  • OCR reference language: for i = 1 to 10 … next i runs 10 times, with i going 1, 2, 3 … 10.
  • Python: for i in range(1, 11):. range stops before the second number.
  • Add step to count in jumps: for i = 0 to 10 step 2 gives 0, 2, 4, 6, 8, 10.
  • Use it when you know how many times to repeat.

Python

for i in range(1, 6):
    print(i)

OCR reference language

for i = 1 to 5
    print(i)
next i

Condition-controlled loops (while)

Before this: Count-controlled loops (for)Comparison operators

  • A condition-controlled loop repeats until a condition changes. Use it when you don't know how many times it will run.
  • while checks the condition at the start. If it is false straight away, the loop never runs.
  • do … until checks the condition at the end, so it always runs at least once. (OCR reference language only.)
  • Make sure something inside the loop can change the condition, or it will loop forever.

while loop (Python)

answer = ""
while answer != "yes":
    answer = input("Ready? ")

do until (OCR reference language)

do
    answer = input("Ready?")
until answer == "yes"

Key words

Say what each one means, then tap to check.

Arithmetic operators

OCRPythonMeaningExample
+ - * /+ - * /add, subtract, multiply, divide7 / 2 = 3.5
MOD%the remainder after dividing17 MOD 5 = 2
DIV//whole number division17 DIV 5 = 3
^**to the power of2 ^ 3 = 8

Uses: MOD 2 tells you if a number is even (the answer is 0). DIV and MOD together turn 135 minutes into 2 hours (135 DIV 60) and 15 minutes (135 MOD 60).

Try it

Change the two numbers. Watch MOD and DIV share out the sweets.

Key points

Arithmetic operators

Before this: Assignment

  • + - * / add, subtract, multiply and divide.
  • MOD gives the remainder: 17 MOD 5 = 2.
  • DIV gives the whole number part of a division: 17 DIV 5 = 3.
  • ^ means to the power of: 2 ^ 3 = 8.
  • In Python, MOD is %, DIV is // and power is **.
  • MOD 2 is a quick way to test for even numbers: if n MOD 2 == 0, n is even.
OperatorExampleResult
/17 / 53.4
DIV17 DIV 53
MOD17 MOD 52
^2 ^ 38

Key words

Say what each one means, then tap to check.

Comparison and Boolean operators

Comparison

==equal to
!=not equal to
< <=less than (or equal to)
> >=greater than (or equal to)

Boolean

ANDboth must be true
ORat least one must be true
NOTflips true and false

A comparison always gives True or False. Boolean operators join comparisons into one bigger condition. Python writes them in lower case: and, or, not.

Watch out: = stores a value (assignment). == compares two values.

Try it

Slide the age. Which conditions are True?

Key points

Comparison operators

Before this: Variables and constants

  • Comparison operators compare two values. The answer is always True or False.
  • == equal to, != not equal to.
  • < less than, <= less than or equal to.
  • > greater than, >= greater than or equal to.
  • Watch the boundary: age > 18 is False when age is 18.

AND, OR and NOT

Before this: Selection (if)

  • AND: true only if both conditions are true.
  • OR: true if at least one condition is true.
  • NOT: flips true to false, and false to true.
  • They join conditions: if age >= 13 AND age <= 19 then checks for a teenager.
  • Python writes them in lower case: and, or, not.

Key words

Say what each one means, then tap to check.

Also useful: Nesting

Exam practice

29 questions in the style of the OCR exam, 61 marks in total. Try each one, then check the mark scheme.

The building blocks

Question 1[2 marks]
Describe the difference between a variable and a constant.
Hint

Which one can change while the program runs?

Describe means say what it is like or what happens. Make each point a separate fact.

2 marks: make 2 separate points.

Show mark scheme
  • A variable's value can change while the program is running…
  • …a constant's value cannot change while the program is running.

1 mark for each point.

Question 2[2 marks]
Identify one variable and one constant in this code.
const MAX_TRIES = 3
tries = 0
while tries < MAX_TRIES
    guess = input("Guess")
    tries = tries + 1
endwhile
Hint

Which name is set with const? Which names are given new values?

Identify means pick out or name the right thing. A short answer is enough.

2 marks: make 2 separate points.

Show mark scheme
  • Variable: tries / guess.
  • Constant: MAX_TRIES.

1 mark for each.

Question 3[2 marks]
Give two reasons for storing the VAT rate as a constant.
Hint

What if the VAT rate changes next year, and it is used in 50 places in the program?

Give means give a short, clear answer. You don't need to explain it.

2 marks: make 2 separate points.

Show mark scheme
  • If the rate changes, it only needs changing in one place.
  • It can't be changed by mistake while the program runs.
  • The name makes the code easier to read.

1 mark each, max 2

Question 4[1 mark]
State the value of x after this code runs.
x = 4
y = x * 2
x = y + 1
Hint

Work line by line. The right-hand side is worked out first, then stored on the left.

State means give a short, clear answer. You don't need to explain it.

1 mark: one answer is enough.

Show mark scheme
  • 9
Question 5[2 marks]
Explain the difference between = and ==.
Hint

One of them stores a value. The other one checks something.

Explain means say what happens and why. Words like “because” and “so” help.

2 marks: aim for 2 things said, such as a point and then the reason for it.

Show mark scheme
  • = assigns / stores a value in a variable.
  • == compares two values to see if they are equal.

1 mark for each.

Question 6[2 marks]
Write code that asks the user for their name and then outputs Hello followed by their name.
Hint

One line to get the name, and one line to output it. Join the text with +.

Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.

2 marks: each part that works earns marks, even if the whole thing isn't perfect.

Show mark scheme
  • Input the name into a variable.
  • Output Hello joined to the name.

Example answer

name = input("What is your name?")
print("Hello " + name)

1 mark for each. Python is fine too.

Question 7[2 marks]
A program uses age = int(input("Age?")). Explain why int() is needed.
Hint

What type of data does input() always give you?

Explain means say what happens and why. Words like “because” and “so” help.

2 marks: aim for 2 things said, such as a point and then the reason for it.

Show mark scheme
  • Input is always a string.
  • int() turns it into an integer so it can be used in maths / compared with numbers.

1 mark for each point.

The three constructs

Question 8[1 mark]
Define the term sequence.
Hint

Think about the order the lines run in.

Define means give the exact meaning in one clear sentence.

1 mark: one answer is enough.

Show mark scheme
  • Instructions are carried out one after another, in order.
Question 9[2 marks]
This code doesn't work. Explain what is wrong and how to fix it.
01 total = price * quantity
02 price = float(input("Price"))
03 quantity = int(input("How many?"))
04 print(total)
Hint

Trace it from line 01. Do price and quantity have values yet?

Explain means say what happens and why. Words like “because” and “so” help.

2 marks: aim for 2 things said, such as a point and then the reason for it.

Show mark scheme
  • total is worked out before price and quantity have values.
  • Move line 01 after line 03 (before the print).

1 mark for the problem, 1 mark for the fix.

Question 10[4 marks]
Write a program that inputs a test score and outputs:
Distinction for 70 or more
Merit for 50 to 69
Pass for 30 to 49
Fail for anything lower.
Hint

Check the highest grade first, then work down with elseif. Test it with 70, 69, 30 and 29.

Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.

4 marks: each part that works earns marks, even if the whole thing isn't perfect.

Show mark scheme
  • Input the score as an integer.
  • Check >= 70 for Distinction.
  • Check >= 50 for Merit and >= 30 for Pass using elseif.
  • Else Fail.

Also accept: Separate ifs with ranges, e.g. score >= 50 AND score < 70.

Don't accept: Checking >= 30 first with no ranges. Every score of 30 or more would then get Pass.

Example answer

score = int(input("Score"))
if score >= 70 then
    print("Distinction")
elseif score >= 50 then
    print("Merit")
elseif score >= 30 then
    print("Pass")
else
    print("Fail")
endif

1 mark for each point, up to 4. Python is fine too.

Question 11[2 marks]
State what this code outputs when temp is 12, and when it is 25.
if temp > 20 then
    print("Hot")
elseif temp > 10 then
    print("Warm")
else
    print("Cold")
endif
Hint

Only the first true branch runs. Check each condition in order.

State means give a short, clear answer. You don't need to explain it.

2 marks: make 2 separate points.

Show mark scheme
  • 12: Warm
  • 25: Hot

1 mark for each.

Question 12[1 mark]
State how many times this loop runs: for i = 2 to 6
Hint

Count them: 2, 3, 4…

State means give a short, clear answer. You don't need to explain it.

1 mark: one answer is enough.

Show mark scheme
  • 5
Question 13[3 marks]
Write a program that outputs the 5 times table from 5 × 1 to 5 × 12, e.g. 5 x 3 = 15.
Hint

A loop from 1 to 12. Each time round, work out 5 × the counter and print it.

Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.

3 marks: each part that works earns marks, even if the whole thing isn't perfect.

Show mark scheme
  • A for loop from 1 to 12.
  • Works out 5 * i.
  • Outputs in the right format.

Also accept: Commas instead of +, e.g. print("5 x", i, "=", 5 * i).

Don't accept: A loop from 0 to 11. That outputs 5 × 0 to 5 × 11.

Example answer

for i = 1 to 12
    print("5 x " + str(i) + " = " + str(5 * i))
next i

1 mark for each point. Python is fine too.

Question 14[1 mark]
State the values of i produced by the Python loop for i in range(0, 10, 2):
Hint

range(start, stop, step) stops before the stop number.

State means give a short, clear answer. You don't need to explain it.

1 mark: one answer is enough.

Show mark scheme
  • 0, 2, 4, 6, 8

Don't accept: Including 10. The range stops before 10.

Question 15[2 marks]
Explain the difference between a while loop and a do … until loop.
Hint

Where is the condition checked: at the start or at the end? What does that mean for running at least once?

Explain means say what happens and why. Words like “because” and “so” help.

2 marks: aim for 2 things said, such as a point and then the reason for it.

Show mark scheme
  • while checks the condition at the start, so it may not run at all.
  • do … until checks at the end, so it always runs at least once.

1 mark for each.

Question 16[4 marks]
Write a program that keeps asking the user for numbers and adds them up. It stops when the user enters 0, then outputs the total.
Hint

You need a total that starts at 0, a loop that stops when 0 is entered, and an input inside the loop.

Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.

4 marks: each part that works earns marks, even if the whole thing isn't perfect.

Show mark scheme
  • Total set to 0 at the start.
  • Loop that repeats until the number is 0.
  • Number input inside the loop and added to total.
  • Total output after the loop.

Also accept: A do … until loop that stops when 0 is entered.

Don't accept: A for loop with a fixed number of goes. The user decides when to stop.

Example answer

total = 0
num = int(input("Number"))
while num != 0
    total = total + num
    num = int(input("Number"))
endwhile
print(total)

1 mark for each point. Python is fine too.

Question 17[1 mark]
Explain why a condition-controlled loop is used to check a password, not a count-controlled loop.
Hint

Do you know how many tries the user will need?

Explain means say what happens and why. Words like “because” and “so” help.

1 mark: one point, with the reason.

Show mark scheme
  • You don't know how many attempts the user will need.

Arithmetic operators

Question 18[3 marks]
State the result of each calculation.
20 MOD 6
20 DIV 6
3 ^ 2
Hint

MOD is the remainder. DIV is how many whole times it goes in. ^ is a power. Try them in the operators table.

State means give a short, clear answer. You don't need to explain it.

3 marks: make 3 separate points.

Show mark scheme
  • 2
  • 3
  • 9

Don't accept: 20 DIV 6 = 3.33. DIV gives the whole number part only.

Don't accept: 3 ^ 2 = 6. ^ means "to the power of", so 3 × 3.

1 mark for each.

Question 19[2 marks]
A program turns a number of minutes into hours and minutes, e.g. 135 minutes is 2 hours 15 minutes. Fill in the two missing operators.
mins = 135
hours = mins ____ 60
left = mins ____ 60
print(hours, "hours", left, "minutes")
Hint

Hours is how many whole 60s fit into 135. Minutes is what is left over.

Complete means fill in every gap.

2 marks: check every part before you finish.

Show mark scheme
  • DIV
  • MOD

Also accept: // and % (Python).

1 mark for each.

Comparison and Boolean operators

Question 20[3 marks]
x = 7. State whether each expression is True or False.
x > 7
x >= 7
x != 3
Hint

Is 7 greater than 7? Is it greater than or equal to 7?

State means give a short, clear answer. You don't need to explain it.

3 marks: make 3 separate points.

Show mark scheme
  • False
  • True
  • True

Don't accept: x > 7 is True. 7 is not greater than 7.

1 mark for each.

Question 21[1 mark]
State the operator that means not equal to.
Hint

It is made of two symbols: one meaning "not" and one meaning "equals".

State means give a short, clear answer. You don't need to explain it.

1 mark: one answer is enough.

Show mark scheme
  • !=

Also accept: <>

Question 22[3 marks]
age = 15 and member = false. State whether each is True or False.
age > 12 AND member == true
age > 12 OR member == true
NOT member
Hint

member is false. AND needs both sides to be true, OR needs at least one, and NOT flips it. Try them on the conditions slider.

State means give a short, clear answer. You don't need to explain it.

3 marks: make 3 separate points.

Show mark scheme
  • False
  • True
  • True

Don't accept: NOT member is False. member is false, so NOT member is true.

1 mark for each.

Question 23[2 marks]
Write a condition that is true when temp is between 18 and 24, including both.
Hint

Two comparisons joined with a Boolean operator. "Including both" means use >= and <=.

Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.

2 marks: each part that works earns marks, even if the whole thing isn't perfect.

Show mark scheme
  • temp >= 18 …
  • … AND temp <= 24

Don't accept: temp > 18 AND temp < 24. It misses 18 and 24.

Don't accept: temp >= 18 OR temp <= 24. It is true for every number.

Example answer

if temp >= 18 AND temp <= 24 then

1 mark for each half.

Mixed questions on the whole page

Question 24[2 marks]
A game stores the number of lives a player has left, and the maximum number of lives, which is always 3. State which should be a variable and which should be a constant, and explain why.
Hint

Which value goes up and down while you play? Which one stays the same?

State means give a short, clear answer. You don't need to explain it.

2 marks: make 2 separate points.

Show mark scheme
  • Lives left is a variable, because it changes during the game.
  • Maximum lives is a constant, because it never changes while the program runs.

1 mark for each.

Question 25[2 marks]
A program needs to check whether a number, num, is a multiple of 7. Write the condition it should use, and explain how it works.
Hint

Think about the remainder when you divide a multiple of 7 by 7.

Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.

2 marks: each part that works earns marks, even if the whole thing isn't perfect.

Show mark scheme
  • num MOD 7 == 0
  • MOD gives the remainder, and a multiple of 7 leaves a remainder of 0.

Also accept: num % 7 == 0 (Python).

1 mark for the condition, 1 mark for the explanation.

Question 26[3 marks]
Name the three basic programming constructs.
Hint

One runs in order, one makes decisions, and one repeats.

Name means a word or a short phrase is enough.

3 marks: make 3 separate points.

Show mark scheme
  • Sequence.
  • Selection.
  • Iteration.

Also accept: Iteration: "repetition" or "loops".

1 mark for each.

Question 27[2 marks]
Explain the difference between count-controlled and condition-controlled iteration.
Hint

Which one knows how many times it will run before it starts?

Explain means say what happens and why. Words like “because” and “so” help.

2 marks: aim for 2 things said, such as a point and then the reason for it.

Show mark scheme
  • Count-controlled repeats a set number of times (e.g. a for loop).
  • Condition-controlled repeats until / while a condition is met (e.g. a while loop).

1 mark for each.

Question 28[2 marks]
Rewrite this code so that it uses one if statement with a Boolean operator.
if age >= 13 then
    if age <= 19 then
        print("Teenager")
    endif
endif
Hint

Both conditions must be true for "Teenager" to print. Which Boolean operator means "both"?

Rewrite means write the code again with the change made. Keep everything else working.

2 marks: each part that works earns marks, even if the whole thing isn't perfect.

Show mark scheme
  • if age >= 13 AND age <= 19 then
  • print("Teenager") inside it, with one endif.

Example answer

if age >= 13 AND age <= 19 then
    print("Teenager")
endif

1 mark for each point. Python is fine too.

Question 29[2 marks]
This loop should output 10, 8, 6, 4, 2. Write the missing first line.
______
    print(i)
next i
Hint

Where does it start and end? It goes down by 2 each time, so what step does it need?

Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.

2 marks: each part that works earns marks, even if the whole thing isn't perfect.

Show mark scheme
  • for i = 10 to 2
  • step -2

Also accept: for i in range(10, 0, -2): (Python).

Example answer

for i = 10 to 2 step -2
    print(i)
next i

1 mark for the start and end values, 1 mark for step -2.