ScholaFly

CS12-01 Computer Science Watch

Variables, constants and assignment

Watch on YouTube

In this lesson

Variables, constants and assignment for GCSE Computer Science, with worked examples and the mistakes examiners report. AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Programming basics 1 of 9. More at https://scholafly.com

By the end: Say what a variable and a constant are (a named place in memory whose value can, or cannot, change while the program runs), declare each and give it a starting value, read an assignment right to left so that score = score + 1 replaces the old value, describe an assignment statement in precise words, and give the reasons named constants are used.

What it covers

  1. 0:00 Intro: variables
  2. 0:36 Variables
  3. 3:41 Store the answer
  4. 8:22 Variables: constants
  5. 11:44 Name the fault in each line

Key words

About this video

Variables, constants and assignment | GCSE Computer Science | CS12-01

Variables, constants and assignment for GCSE Computer Science, with worked examples and the mistakes examiners report.
AQA 8525, Edexcel 1CP2 and OCR J277 GCSE. Programming basics 1 of 9.
More at https://scholafly.com

Watch first: CS10-05 - https://youtu.be/Ioa4-no7NkE
Video code: CS12-01 (search "ScholaFly CS12-01")

#GCSEComputerScience #ProgrammingBasics #Variables

For teachers
This GCSE Computer Science lesson teaches variables, constants and assignment. By the end, students should be able to say what a variable and a constant are (a named place in memory whose value can, or cannot, change while the program runs), declare each and give it a starting value, read an assignment right to left so that score = score + 1 replaces the old value, describe an assignment statement in precise words, and give the reasons named constants are used. It works through four worked examples and the mistakes examiners report.

Exam board specification references:
AQA 8525
- 3.2.2
Edexcel 1CP2
- 1.2.2
- 6.3.2
OCR J277
- 2.2.1

Read the transcript

How do you change one number that sits in forty places? Picture an online shop's checkout code with the VAT rate, nought point two, typed into forty lines. Editing all forty by hand is the obvious answer, and the trap: miss one, and some customers pay the old price. The basket total, though, changes with every item, so code needs a different tool for each kind of number.

A variable is a named place in memory that holds a value, and that value can change while the program runs. The name is how the program finds that value again, wherever it needs it. The memory here is RAM, where a running program keeps its data. Think of a variable as a box with a label on it: the label is the name, and the box holds one value at a time. The name, called an identifier, has one rule you must keep. It is a single unbroken word, with no gaps in it. So how does a computer read a name typed as my score, with a space? As two separate names, my and score. Write it as one word, myScore, with a capital S. Storing a value in a variable is called assignment. In Python the line reads score, an equals sign, then five, and it stores five in the box named score. That equals sign does not mean equal to. It means store. One course's own pseudo-code draws it as an arrow pointing left, and the video Pseudocode shows that style. Checking whether two values are equal is a different job, with a double equals sign, covered in the video Relational operators. So an assignment is read right to left. First, everything on the right of the equals sign is worked out. Then store the answer in the box named on the left, and the old value is gone. Here are three lines, run in order. Line one, score equals five. Line two, score equals score plus one. Line three, score equals score times two. Read as maths, line two is impossible. Read as code, it is an instruction. Line two adds one. Right-hand side first: score holds five, and five plus one is six. Then six is stored in score, and the five is gone. Now line three, read right to left. What does score hold after it runs? Twelve. Six times two is twelve, and twelve replaces the six in score.

Storing the answer is the second half of every assignment, and it is the half that can go missing. Leave the left-hand side off. A line that only says score plus one works out six, and then nothing catches it. The six is lost, and score still holds five. One examiner's report, on a question about adding one to a score, says this. Most candidates were able to temporarily add one to the value of score, by using code such as score plus one. Fewer were able to assign this new value to the variable of score and therefore fully answer the question. Answers such as score equals score plus one, score plus plus or score plus equals one were all accepted. So the fix is to finish the line. Whenever a value changes, write the variable's name and the equals sign in front of the working, and the new value is stored. Adding to a variable is so common that Python has a short form. Score plus equals one does the same job as score equals score plus one. Here are two lines that look almost the same, and score starts at five. Line A is score plus equals one. Line B is score equals plus one. Which line leaves score holding six: A, B, or both? Only A. It adds one to the five and stores six. Now say why line B leaves score holding one. Read B right to left. The right-hand side is plus one, a positive one, so one is stored and the five is wiped out. One examiner's report records this exact line. One notable response that was not allowed was score equals plus one. This statement assigns the value of positive one to score, overwriting the previous value held. The fix is the order of the two symbols: the plus first, then the equals. A slip like this runs without any error message, which is why it only shows when you check what the code stored. Some questions ask you to describe an assignment in words. Take total equals total plus price. Description A: it assigns something to the variable total. Description B: it stores the value of total plus price in total. Description C: it checks that total is the value of total plus price. Which description of total equals total plus price is the precise one: A, B or C? B. Only B says what is worked out and where it goes. A uses the empty word something. C describes a check, and an equals sign in Python never checks anything. One examiner's report says this about that question. Part one point four asked for a description of an assignment statement, this was well answered with just over sixty-five per cent of students gaining the mark available. Many students who did not gain the mark gave answers that were too vague such as assigning something to a variable. The fix is one precise sentence. An assignment stores a value in a variable, replacing whatever it held before. Value and variable are the specific terms that sentence needs.

A constant is a named value that is set once and cannot change while the program runs. It is the same labelled box, with a padlock on the lid. The value goes in when the constant is created, and no later line may replace it. Creating a variable or a constant is called declaring it, and giving it its first value is called initialising it. Some languages need a separate declaration line. Python does not, so the first assignment declares the name. Python has no padlock of its own. The habit is a name in capitals, VAT RATE, with a low dash joining the two words so it stays one name. The capitals are a promise that no line will change it. One course's reference language writes the word const in front instead. If you answer in C sharp or VB dot NET, those languages write the same two ideas in their own way, and the boxes behave exactly the same. Here is a game that starts with LIVES equals three, in capitals. Twenty lines further down, it says LIVES equals five. LIVES is in capitals. What is wrong with that later line? It treats a constant like a variable. A constant is fixed for the whole run, so if the number must change, it should have been a variable all along. Back to the checkout, cut down to three lines that each multiply a price by nought point two. Each of those nought point twos is a literal, a value typed straight into the code. The rewrite puts one line at the top: VAT RATE, in capitals, equals nought point two. The three lines below use the name in place of the number. Why is the rewrite with the named constant better? Give two reasons. One edit at the top updates every use at once, so none can be missed. And the value is safe from accidental changes while the program runs. There is a third reason too. The name says what the number means, which nought point two on its own never does. One examiner's report, listing its most frequently missed marks, names the use of literals rather than the constants provided. The fix is a habit. When a question gives you a constant, use its name every time that value is needed, and never retype the number.

Four lines of code follow, and each one breaks a different rule from this video. Line one: high score, with a gap in the name, equals zero. Line two: LIVES equals five, after LIVES was set to three. Line three: score equals plus one, meant to add one. Line four: cost equals price times nought point two, when the constant VAT RATE already exists. Pause and name the fault in each of the four lines. The first: a gap splits the name in two, so write highScore as one unbroken word. Line two, LIVES is a constant, so nothing may change it. If it has to change, make it a variable instead. Line three, the symbols are the wrong way round. Plus equals adds one, but equals plus stores a positive one. Line four, the literal should be the constant, so it reads cost equals price times VAT RATE.

Right to left, the stored answer and the padlock: each question here uses code this video has not run. x holds four. What does x hold after x equals x plus x? Eight. Four plus four is worked out first, then eight replaces the four. Try the symbols the other way: lives is three, then lives equals minus one. What is lives? Minus one. The equals comes first, so a negative one is stored and the three is wiped. Minus equals would have left two. Another one, in words: what does total equals total minus discount do? It works out total minus discount and stores the result in total, replacing the old value. One more, back at the checkout: is the basket total a variable or a constant, and why? A variable. It changes with every item added, and a constant can never change while the program runs. The VAT rate is the checkout's constant, so a new rate is one edit, not forty.

If reading code right to left has clicked, give this one a thumbs up, so you know you can skip it next time. If it has not, save it, then type three of these lines into Python and watch the value in the box change.

Next in the chapter: Data types. Hopefully that's next in your feed, but if not, search for ScholaFly C S twelve, oh two.

Every other video is on scholafly.com too, easy to search by video code or by keyword. Whatever you're revising, go there and have a look, or, watch the next video.

Related terms

For: AQA GCSE 8525, Edexcel GCSE 1CP2, OCR GCSE J277

On the specification

BoardSpecStatement
AQA GCSE 85253.2.2Programming concepts
Edexcel GCSE 1CP21.2.2Algorithms
Edexcel GCSE 1CP26.3.2Write programs that make appropriate use of variables and constants.
OCR GCSE J2772.2.1Programming fundamentals
For teachers

This GCSE Computer Science lesson teaches variables, constants and assignment. By the end, students should be able to say what a variable and a constant are (a named place in memory whose value can, or cannot, change while the program runs), declare each and give it a starting value, read an assignment right to left so that score = score + 1 replaces the old value, describe an assignment statement in precise words, and give the reasons named constants are used. It works through four worked examples and the mistakes examiners report.