Variables, constants, input, output and assignment

GCSE Computer Science (OCR) · OCR J277 · Algorithms and programming (Paper 2) › Programming fundamentals › Programming fundamentals

66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.

Or take a quick quiz: 10 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.

Revision notes

Variables and constants store data in a program; assignment gives them values; input and output let the program talk to the user. These ideas appear in almost every code question, and you may be asked to define the terms, spot them in code or write short programs that use them.

Key facts

  • Variable: named memory location whose value can change while the program runs
  • Constant: named value that cannot change while the program runs (const in ERL)
  • Assignment: variable = expression (the right side is worked out first)
  • = assigns a value; == compares two values
  • input() always returns a string: cast with int() or float() for numbers
  • print() outputs to the screen; use str() to join numbers to text
  • Use meaningful identifiers, such as totalCost, not x

Notes

Variables and constants

  • A variable is a named location in memory that stores a value. The value can change while the program is running.
  • A constant is a named value that cannot change while the program is running. In ERL it is declared with const, for example const VAT = 0.2.
  • Why use a constant: its value cannot be changed by mistake while the program runs, and if the value ever needs updating (for example a new VAT rate) the programmer changes it in just one place in the code.
  • Use meaningful names (identifiers), such as totalCost rather than x, so the code is easy to understand.

Assignment

diagram
  • Assignment gives a variable a value using =, for example score = 0.
  • The right-hand side is worked out first, then the result is stored in the variable on the left. So score = score + 10 adds 10 to the current score.
  • score10score20score = score + 10right side first: 10 + 10 = 20then 20 is stored in score (the old 10 is replaced)
    Assignment: work out the right-hand side, then store it in the variable on the left
  • Do not confuse = (assignment) with == (comparing two values).

Input and output

  • Input: name = input("Enter your name") shows the message and stores what the user types.
  • Input is always a string, so cast it to use it as a number: age = int(input("Enter your age")).
  • Output: print("Hello " + name) displays text on the screen. Use str() to join a number to a string.
  • Example: a simple program.
    const PRICE = 4.5
    tickets = int(input("How many tickets?"))
    cost = tickets * PRICE
    print("Total: £" + str(cost))

How to answer each type of question

Explain why a constant is used

2 marksGrade 4
  1. Say that the value stays the same while the program runs, so it cannot be changed by accident.
  2. Say that it only needs changing in one place in the code if it is ever updated.

Example. A program uses the value 0.2 for VAT in several places. Explain why VAT should be declared as a constant rather than a variable. [2]

Show the model answerHide the model answer
The value does not need to change while the program runs, so declaring it as a constant stops it being changed by accident (1). If the VAT rate changes, it only needs to be updated in one place in the code (1).

State the values after a sequence of assignments

2 to 3 marksGrade 5
  1. Work through one line at a time, keeping a note of each variable's current value.
  2. Always use the value a variable has at that moment, not its starting value.
  3. If asked what the code does, describe the overall effect.

Example. State the final values of a and b, and describe what the code does.

a = 4
b = 7
a = a + b
b = a - b
a = a - b
[3]

Show the model answerHide the model answer
a = 7 (1), b = 4 (1). It swaps the values of a and b (1). (a becomes 11, then b = 11 − 7 = 4, then a = 11 − 4 = 7.)

Write a short input, process, output program

3 marksGrade 6
  1. Input the data and cast it to the right type.
  2. Do the calculation and store the result in a variable.
  3. Output the result.

Example. Write a program that asks the user for a temperature in degrees Celsius and outputs it in degrees Fahrenheit, using F = C × 1.8 + 32. [3]

Show the model answerHide the model answer
c = float(input("Enter temperature in Celsius"))
f = c * 1.8 + 32
print(f)
Input stored in a variable and cast to a real number (1); correct calculation stored in a variable (1); outputs the result (1).

Don’t lose marks

  • Saying a constant 'can never be changed': the programmer can edit it in the code; it just cannot change while the program runs.
  • Writing assignments backwards, such as 10 = score.
  • Doing arithmetic on input without casting it: "5" + "3" gives "53", not 8.
  • Using = when you mean == in a condition.
  • Joining a number to a string without str().

More tips

Memory tricks

  • Constant = Can't change (while running). Variable = Varies.
  • Read assignment from right to left: 'work this out, then put it in there'.
  • When you see input() in a calculation, check for int() or float() around it.

Exam technique

  • Definitions of variable and constant should mention 'while the program is running'.
  • If a question gives variable names, use exactly those names in your code.
  • Initialise variables (for example total = 0) before using them in a calculation.

What each grade needs

What you need to be able to do, from the first marks up to the top grade.

  1. Grade 3
    Define a variable and a constantA variable's value can change while the program runs; a constant's value cannot.
  2. Grade 4
    Identify variables, constants and assignments in codePoint to the lines that declare a constant, assign a value, take input or give output.
  3. Grade 4
    Explain why a constant is usedIt cannot be changed by mistake, and it only needs updating in one place in the code.
  4. Grade 5
    Trace a sequence of assignmentsWork out each variable's value line by line, remembering the right side is worked out first.
  5. Grade 6
    Write code that inputs, processes and outputsTake input, cast it to the right type, calculate a result and output it.

Quick recall

Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.

Look at this program.
01 const VAT = 0.2
02 price = float(input("Enter price"))
03 total = price + price * VAT
04 print(total)
Identify the constant used in the program.
VAT
Look at this program.
01 name = input("What is your name?")
02 age = int(input("How old are you?"))
03 age = age + 1
04 print("Next year " + name + " will be " + str(age))
State the output if the user enters Sam and then 14.
Next year Sam will be 15
Look at this program.
01 name = input("Enter your name")
02 const GREETING = "Welcome"
03 print(GREETING + ", " + name)
Identify the constant used in the program.
GREETING
Describe one difference between a variable and a constant.
A variable’s value can change as the program runs, but a constant’s value stays the same.
Give the name of the command used in the OCR Exam Reference Language to display a value on the screen.
print
Give one reason why a programmer should use meaningful names for variables.
It makes the program easier to read and understand.

Sample questions

Written for this site in the style of OCR exam questions. They are not taken from real past papers.

Question 1Easy4 marks
Look at this program.
01 const VAT = 0.2
02 price = float(input("Enter price"))
03 total = price + price * VAT
04 print(total)
(a) State what is meant by a variable.
[1]
(b) State what is meant by a constant.
[1]
(c) Identify the constant used in the program.
[1]
(d) Identify the line number of the line that outputs data.
[1]
Show the answer and mark scheme
(a)
  • a named location in memory that stores a value that can change while the program is running
(b)
  • a named value that cannot change while the program is running
(c) Answer: VAT
  • VAT
(d) Answer: 04
  • 04 / line 4
Question 2Medium5 marks
A gym uses this program to work out the cost of a membership.
01 const MONTHLY_FEE = 25
02 months = int(input("Number of months"))
03 cost = MONTHLY_FEE * months
04 if months >= 12 then
05    cost = cost - 30
06 endif
07 print(cost)
(a) State the output when the user enters 12.
[1]
(b) Explain why MONTHLY_FEE has been declared as a constant rather than as a variable.
[2]
(c) Identify one other value in the program that could be declared as a constant.
[1]
(d) A programmer adds the line MONTHLY_FEE = 30 near the end of the program. Explain what would happen.
[1]
Show the answer and mark scheme
(a) Answer: 270
  • 270
(b)
  • its value does not need to change while the program runs / it cannot be changed by mistake
  • if the fee changes, it only needs to be updated in one place, making the program easier to maintain
(c)
  • 30 (the discount) / 12 (the number of months needed for the discount)
(d)
  • an error would be reported / the program would not run, because the value of a constant cannot be changed
Question 3Hard7 marks
This program should work out the interest earned on a savings account. It contains two errors.
01 const RATE = 0.05
02 balance = float(input("Enter balance"))
03 years = int(input("Enter years"))
04 for y = 1 to years
05    interest = balance * RATE
06    balance = balance + interest
07 next y
08 RATE = 0.06
09 print("Final balance: " + str(balance))
10 print("Total interest: " + str(totalInterest))
(a) Identify the two errors in the program and explain why each one is an error.
[4]
(b) Describe the changes needed so that line 10 outputs the total interest earned.
[2]
(c) The errors are corrected. State the output of line 09 when the user enters 1000 and 2.
[1]
Show the answer and mark scheme
(a)
  • line 08
  • the value of a constant cannot be changed while the program is running
  • line 10
  • totalInterest is used but has never been declared / given a value
(b)
  • set totalInterest to 0 before the loop
  • inside the loop, add interest to totalInterest (totalInterest = totalInterest + interest)
(c) Answer: Final balance: 1102.5
  • Final balance: 1102.5

More in Programming fundamentals

Stuck? Get 1-to-1 help.

Chhetri Academy tutors GCSE and A level Maths and Science online, with a free 30-minute trial lesson.