Teachers' Portal  /  A-Level Computer Science 9618  /  Programming  /  Variables, constants and built-in functions
Lesson pack · A-Level Computer Science 9618 · 11.1 · AS Level

Variables, constants and built-in functions

Name it, store it, use it. Every program starts with data: variables that change, constants that do not, and expressions that combine them.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Programming
Syllabus reference
Cambridge International AS and A Level Computer Science 9618, 11.1
Level
AS Level
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 1 of 4 in Programming; leads into Selection and iteration constructs
Central model
Fig. 1: converting a program flowchart into pseudocode, one symbol at a time
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Declare, initialise, assign: Exam questions give you a design as a program flowchart or as structured English and ask for pseudocode.
  • Operators and expressions: An expression is anything that evaluates to a single value.
  • Built-in functions: Built-in functions and library routines are subroutines that are already written, tested and ready to use: they save time and reduce errors.
Key points to leave on the board
  • Constant: CONSTANT Name = literal, declared at the start, never assigned again.
  • Division: / gives a REAL; DIV gives the whole-number quotient; MOD gives the remainder.
  • Strings: positions start at 1; MID(s, start, length); join with &.
  • Random integers: INT(RAND(n)) gives 0 to n − 1; add the lowest value you want.
The core visual model

One picture every idea returns to

A variable is a named memory location whose value can change while the program runs; declare it with DECLARE Name : TYPE and give it a value with the assignment operator ←. A constant is declared once with CONSTANT Name = value, using a literal only, and can never be changed. Expressions combine values with arithmetic operators (+ - * / DIV MOD), relational operators and the logical operators AND, OR, NOT. Built-in functions and library routines are pre-written, pre-tested subroutines such as LENGTH, MID and INT.

each flowchart symbol becomes one pseudocode statement STARTINPUT Celsius F ← Celsius * 9 / 5 + 32OUTPUT FSTOP DECLARE Celsius : REAL DECLARE F : REAL INPUT Celsius F ← Celsius * 9 / 5 + 32 OUTPUT F START and STOP need no statement; declarations come first Celsius = 20 gives F = 68
Fig. 1: converting a program flowchart into pseudocode, one symbol at a time
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, Name it, store it, use it.. Every program starts with data: variables that change, constants that do not, and expressions that combine them. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Declare, initialise, assignExam questions give you a design as a program flowchart or as structured English and ask for pseudocode. Model the worked example Structured English to pseudocode on the board, one line at a time. Your turn: With the ticket program above, what is output when 4 is input?Whole class, then pairs
13 to 21 minTeach and check: Operators and expressionsAn expression is anything that evaluates to a single value. Model the worked example Evaluate step by step on the board, one line at a time. Your turn: Seconds holds 437. Write two assignment statements that set Minutes and Secs so that 437 seconds is shown as 7 minutes 17 seconds.Pairs
21 to 30 minTeach and check: Built-in functionsBuilt-in functions and library routines are subroutines that are already written, tested and ready to use: they save time and reduce errors. Project tab 7 of the lab (9618 pseudocode: MID and string handling) and let a student drive it while the class predicts. Model the worked example Build a username on the board, one line at a time. Your turn: Explain why INT(RAND(6)) + 1 always gives a whole number from 1 to 6.Cooperative Concept Mapping
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 21 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Built-in functions, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Cooperative Concept Mapping

Teams receive the key terms on cards and arrange them on a large sheet, drawing and labelling the links. Compare maps between teams before showing the model map.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
CONSTANT VAT = Price * 0.2 is not allowed: a constant must be a literal.And a constant can never appear on the left of ← later in the program.
17 / 5 is 3.4, not 3.If a question says the result must be an INTEGER, the mark is for DIV (or INT), not /.
In the 9618 guide UCASE and LCASE take a single CHAR, and string positions start at 1.To change a whole string, loop through it with MID one character at a time.
Worksheet

Variables, constants and built-in functions: worksheet

6 questions, 21 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · recall[2 marks]

Explain the difference between a variable and a constant.

Q2 · calculate[4 marks]

A holds 23 and B holds 4. Give the value of each expression.

(a) A DIV B (b) A MOD B (c) A / B (d) (A > 20) AND (B MOD 2 = 1)

Q3 · convert[4 marks]

Write pseudocode for this structured English. Declare everything you use.

1. SET Pi to 3.142 (this value never changes)
2. INPUT the radius of a circle
3. CALCULATE the area as Pi multiplied by the radius squared
4. OUTPUT a message and the area

Q4 · apply[3 marks]

Code holds the string "CS9618-AS". State the value returned by:

(a) LENGTH(Code) (b) MID(Code, 3, 4) (c) RIGHT(Code, 2)

Q5 · write[5 marks]

A full name such as Ada Lovelace contains exactly one space. Write pseudocode that inputs a full name and outputs the initials, for example AL. Use the functions LENGTH and MID.

Q6 · write[3 marks]

Write pseudocode to generate a random whole number from 10 to 20 inclusive, store it in Number, and output Even or Odd as appropriate.

Answer key and marking notes
Q1 · 2 marks
  • A variable is a named memory location whose value can change while the program runs.
  • A constant is given a (literal) value when it is declared and that value cannot change while the program runs.
Q2 · 4 marks
  • (a) 5
  • (b) 3
  • (c) 5.75
  • (d) FALSE: A > 20 is TRUE but 4 MOD 2 = 0, so B MOD 2 = 1 is FALSE, and TRUE AND FALSE is FALSE.
Q3 · 4 marks
  • CONSTANT Pi = 3.142
  • DECLARE Radius : REAL and DECLARE Area : REAL
  • INPUT Radius and Area ← Pi * Radius * Radius
  • OUTPUT "Area is ", Area
Q4 · 3 marks
  • (a) 9
  • (b) "9618"
  • (c) "AS"
Q5 · 5 marks
  • Declare FullName, Initials (STRING) and Index (INTEGER); INPUT FullName.
  • Initials ← MID(FullName, 1, 1) to take the first letter.
  • FOR Index ← 2 TO LENGTH(FullName) ... NEXT Index.
  • IF MID(FullName, Index, 1) = " " THEN Initials ← Initials & MID(FullName, Index + 1, 1) with ENDIF.
  • OUTPUT Initials after the loop.
Q6 · 3 marks
  • Number ← INT(RAND(11)) + 10 (RAND(11) gives 0 to below 11, INT gives 0 to 10).
  • IF Number MOD 2 = 0 THEN
  • OUTPUT "Even" ELSE OUTPUT "Odd" ENDIF.
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

Which statement about a constant in 9618 pseudocode is correct?

  • AIt is given a literal value when it is declared, and that value cannot change while the program runs
  • BIt can be given its value by an expression that uses other constants, such as Rate * 2
  • CIt must be given a new value with the assignment operator before it is first used
  • DIt is declared with DECLARE and a data type, in exactly the same way as a variable
Q21 mark

A holds 29 and B holds 6. What is the value of A DIV B + A MOD B?

  • A9
  • B4
  • C10
  • D9.83
Q31 mark

Word holds the string "ALGORITHM". What is the value of MID(Word, 3, 4) & RIGHT(Word, 2)?

  • A"GORIHM"
  • B"GOHM"
  • C"ORITHM"
  • D"LGORHM"
Q41 mark

Which statement always stores a random whole number from 5 to 12 inclusive in N?

  • AN ← INT(RAND(8)) + 5
  • BN ← INT(RAND(12)) + 5
  • CN ← INT(RAND(7)) + 5
  • DN ← INT(RAND(8)) + 4
Q51 mark

Seconds holds 7384. This pseudocode is run:

H ← Seconds DIV 3600
M ← (Seconds MOD 3600) DIV 60
S ← Seconds MOD 60
OUTPUT H, ":", M, ":", S
What is output?

  • A2:3:4
  • B2:123:4
  • C2:3:184
  • D2.05:3.07:4
Q61 mark

Which numbered line is not valid 9618 pseudocode?

01  CONSTANT MaxScore = 100
02  CONSTANT HalfScore = MaxScore / 2
03  DECLARE Score : INTEGER
04  Score ← INT(RAND(MaxScore)) + 1
05  OUTPUT "Score: ", Score

  • ALine 02
  • BLine 04
  • CLine 01
  • DLine 05
Quiz key
Q1: A

CONSTANT Name = value takes a literal only, and the value is fixed for the whole run. An expression such as Rate * 2 is not allowed after the =. A constant can never appear on the left of ← later in the program, and it is declared with CONSTANT, not DECLARE with a data type.

Q2: A

29 DIV 6 = 4 (because 4 × 6 = 24) and 29 MOD 6 = 5 (29 − 24), so the sum is 9. The value 4 is the DIV part alone. 10 comes from rounding 4.83 up to 5 instead of truncating. 9.83 uses / (giving 4.83) instead of DIV.

Q3: A

Position 3 is G, and four characters from there are G, O, R, I. RIGHT(Word, 2) gives "HM", and & joins them: "GORIHM". "GOHM" treats 3 and 4 as start and end positions. "ORITHM" counts positions from 0. "LGORHM" starts one character too early, at position 2.

Q4: A

There are 8 values from 5 to 12. INT(RAND(8)) gives 0 to 7, and adding 5 gives 5 to 12. INT(RAND(12)) + 5 can reach 16. INT(RAND(7)) + 5 stops at 11, so 12 is never produced. INT(RAND(8)) + 4 gives 4 to 11.

Q5: A

7384 DIV 3600 = 2 and 7384 MOD 3600 = 184. Then 184 DIV 60 = 3, and 7384 MOD 60 = 4 (7384 = 123 × 60 + 4), so the output is 2:3:4. 123 is Seconds DIV 60, the total number of minutes, ignoring the hours already taken out. 184 is the remainder after the hours, not the seconds. 2.05 and 3.07 come from using / instead of DIV.

Q6: A

A constant must be given a literal value, never an expression or another constant, so line 02 is invalid (it would have to be CONSTANT HalfScore = 50). Line 04 is fine: a constant can be used inside an expression. Line 01 declares a constant with a literal correctly, and line 05 outputs a string and a variable separated by a comma, which OUTPUT allows.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.