IB Computer Science · B2 · Programming
B2.1 Programming fundamentals
Explain variables, constants and data types, use arithmetic and logical operators, and trace a short program to predict its output.
1/6A variable stores a value that can change
- A variable stores a value that can change
- A constant keeps one value for the whole program
- Numbers are stored as integers or reals
- Booleans and strings store truth values and text
- Operators combine values to produce new values
- Tracing a program shows how its values change
- Check your recall
- Practice: 10 questions
Part 1 of 6: A variable stores a value that can change
A game scoreboard changes as play goes on. A program needs a named place to hold such a value. That place is a variable. Each variable has one name, and the program uses that name whenever it needs the value.
Storing a value under a name is called assignment. In these notes, gives score the value 0. A later line such as replaces it with the old value plus 10.
Key terms · tap one for its definition
variable
A variable is a named storage location whose value can change while a program runs.
assignment
Assignment is an instruction that stores a value, or the result of an expression, in a named variable.
Part 2 of 6: A constant keeps one value for the whole program
Some values never change while a program runs. The value of pi and the maximum number of players are two examples. Such a value is stored as a constant.
A constant is given its value once, usually at the start. Its name is often written in capitals, such as . The program cannot then change it by mistake.
A variable compared with a constant
- Feature
- Value can change while running
- Variable
- Yes
- Constant
- No
- Feature
- Set by
- Variable
- Assignment, at any point
- Constant
- One assignment, at the start
- Feature
- Example
- Variable
- Constant
Constants also make code easier to read. If a limit changes, you edit one line instead of many.
Key term
constant
A constant is a named value that is set once and cannot be changed while the program runs.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: C · Worked explanation
Part 3 of 6: Numbers are stored as integers or reals
Every value has a type, so the computer knows how to store and use it. A data type states what kind of value a variable holds and which operations are allowed. The type is chosen when a variable is first created.
An integer is a whole number, such as 42 or −7. Use it for counting. A real number can have a fractional part, such as 3.14 or 1.62. Use it for measuring.
An integer compared with a real
- Feature
- Values stored
- Integer
- Whole numbers only
- Real
- Numbers with a fractional part
- Feature
- Example
- Integer
- 42, −7
- Real
- 3.14, 1.62
- Feature
- Typical use
- Integer
- Counting items
- Real
- Measuring a height of 1.62 m
Key terms · tap one for its definition
data type
A data type is a classification that defines the kind of value a variable can hold and the operations that can be applied to it.
integer
An integer is a data type for whole numbers, positive, negative or zero, with no fractional part.
real
A real is a data type for numbers that may include a fractional part.
Part 4 of 6: Booleans and strings store truth values and text
Not every value is a number. Many programs must store yes-or-no answers and words.
A Boolean has only two possible values, true or false. It records the answer to a question such as whether a user is logged in.
A string is a sequence of characters written in quotation marks, such as . Concatenation joins strings end to end, so gives .
Key terms · tap one for its definition
Boolean
A Boolean is a data type with exactly two possible values, true and false.
string
A string is a data type for a sequence of characters, such as letters, digits and symbols.
concatenation
Concatenation is the joining of two or more strings end to end to form a longer string.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: B · Worked explanation
Part 5 of 6: Operators combine values to produce new values
Variables become useful when a program can calculate with them. An arithmetic operator performs a calculation. The operators are +, −, *, /, mod and div. Here mod gives the remainder after division, and div gives the whole-number quotient.
Calculations follow the usual order. Brackets come first, then *, /, mod and div, then + and −. So 17 mod 5 is 2, and 17 div 5 is 3.
Worked example
Evaluating an expression with mod and div
- Work out the first bracket: 17 mod 5 is 2, since 17 = 3 × 5 + 2.
- Work out the second bracket: 17 div 5 is 3.
- Multiply: .
- Add: .
Answer: The value of the expression is 8.
Comparisons such as give Boolean values. A logical operator combines them. AND is true only when both sides are true. OR is true when at least one side is true. NOT reverses a value.
Key terms · tap one for its definition
arithmetic operator
An arithmetic operator is a symbol that performs a calculation on numbers, such as +, −, *, /, mod or div.
logical operator
A logical operator is an operator, such as AND, OR or NOT, that combines or reverses Boolean values to give a Boolean result.
Part 6 of 6: Tracing a program shows how its values change
Once you can read each line, you can follow a whole program by hand. A trace table records the value of each variable after every line is run. Start with every variable empty, because nothing has a value before its first assignment.
Follow the lines in order and change only the variable named on each line. Values not named stay as they were.
Worked example
Tracing a short program
- Line 1 sets total to 0.
- Line 2 sets count to 3.
- Line 3 sets total to .
- Line 4 sets count to .
- Line 5 outputs the current values of total and count.
Answer: The output is 6 and 2.
Trace table for the program
- After line
- 1
- total
- 0
- count
- empty
- After line
- 2
- total
- 0
- count
- 3
- After line
- 3
- total
- 6
- count
- 3
- After line
- 4
- total
- 6
- count
- 2
Key term
trace table
A trace table is a table that records the value of each variable after every line of a program is executed.
Check your recall[15 marks]
6 written questions. Answer each one in your head or on paper, then open the mark scheme.
- 1.Distinguish[2 marks]
Distinguish between a variable and a constant.
Show mark schemeHide mark scheme
Model answer
A variable holds a value that can change while the program runs. A constant holds a value that is set once and cannot change.
- 1 mark for stating that the value of a variable can change while the program runs
- 1 mark for stating that the value of a constant is fixed and cannot change
- 2.State[2 marks]
State two advantages of using a constant instead of typing the same value many times.
Show mark schemeHide mark scheme
Model answer
Any two from: the code is easier to read because the value has a name; the value can be changed in one place only; the value cannot be changed by mistake.
- 1 mark for stating that the name makes the code easier to read
- 1 mark for stating that the value only needs changing in one place
- 1 mark for stating that the value cannot be changed accidentally
- 3.State[4 marks]
State the most suitable data type for each of these values: a person’s age in whole years; a height of 1.62 m; whether a user is logged in; a surname.
Show mark schemeHide mark scheme
Model answer
Age in whole years is an integer. A height of 1.62 m is a real. Whether a user is logged in is a Boolean. A surname is a string.
- 1 mark for integer for the age
- 1 mark for real for the height
- 1 mark for Boolean for logged in
- 1 mark for string for the surname
- 4.Calculate[2 marks]
Calculate the value of .
Show mark schemeHide mark scheme
Model answer
23 mod 4 is 3, since 23 = 5 × 4 + 3. 23 div 4 is 5. The total is .
- 1 mark for finding that 23 mod 4 is 3 and 23 div 4 is 5
- 1 mark for adding to give 8
- 5.Determine[3 marks]
Determine the output of this program.
Show mark schemeHide mark scheme
Model answer
a becomes . b becomes . The output is 1 and 7.
- 1 mark for finding that a becomes 1
- 1 mark for finding that b becomes 7 using the new value of a
- 1 mark for stating the output as 1 and 7
- 6.Explain[2 marks]
Explain why is a valid instruction in a program but not a true statement in algebra.
Show mark schemeHide mark scheme
Model answer
In a program, = is assignment. The instruction adds 1 to the current value of x and stores the result back in x, so the value changes.
- 1 mark for stating that = means assignment, not equality
- 1 mark for explaining that the new value is calculated from the old value and stored in x

Practice this topic
10 quick-fire multiple-choice questions, easy to hard. Tap an answer to see if you're right and the worked explanation. (The written questions with mark schemes are under “Check your recall” above.)
0 right0 of 10 answered
Includes the 2 quick checks in the note above.
EasyState[1 mark]
Show answer and explanation
Answer: C · Worked explanation
MediumCalculate[1 mark]
Show answer and explanation
Answer: A · Worked explanation
MediumDetermine[1 mark]
Show answer and explanation
Answer: B · Worked explanation
Medium[1 mark]
Show answer and explanation
Answer: C · Worked explanation
Medium[1 mark]
Show answer and explanation
Answer: C · Worked explanation
HardDetermine[1 mark]
Show answer and explanation
Answer: A · Worked explanation
HardCalculate[1 mark]
Show answer and explanation
Answer: C · Worked explanation
HardDeduce[1 mark]
Show answer and explanation
Answer: B · Worked explanation