Scribe logoScribeNotes & practice
Sign inStart free
  1. Notes
  2. ›IB Computer Science
  3. ›B2 Programming
  4. ›B2.1 Programming fundamentals

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.

  • 5 min read
  • 6 parts
  • 10 questions
  • Draft
Start a Quick 5 on this topic→Jump to the 10 questions
1/6A variable stores a value that can change
  1. 1A variable stores a value that can change
  2. 2A constant keeps one value for the whole program
  3. 3Numbers are stored as integers or reals
  4. 4Booleans and strings store truth values and text
  5. 5Operators combine values to produce new values
  6. 6Tracing a program shows how its values change
  7. Check your recall
  8. Practice: 10 questions
1

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, score = 0\texttt{score = 0}score = 0 gives score the value 0. A later line such as score = score + 10\texttt{score = score + 10}score = score + 10 replaces it with the old value plus 10.

Figure 1. Assigning to score replaces the old value with a new one. The name stays the same.

Common mistake

The symbol = in a program is an instruction, not a claim of equality. To compare two values, these notes use ==\texttt{==}==.

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 1 done

2

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 MAXPLAYERS\texttt{MAXPLAYERS}MAXPLAYERS. The program cannot then change it by mistake.

A variable compared with a constant

FeatureVariableConstant
Value can change while runningYesNo
Set byAssignment, at any pointOne assignment, at the start
Examplescore\texttt{score}scoreMAXPLAYERS\texttt{MAXPLAYERS}MAXPLAYERS
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
score\texttt{score}score
Constant
MAXPLAYERS\texttt{MAXPLAYERS}MAXPLAYERS

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.

Part 2 done

Quick check

One question on the part you just read

Easy

Which data type is most suitable for a value that is either true or false?
Show answer and explanation

Answer: C · Worked explanation

A Boolean has exactly two possible values, true and false, so it records yes-or-no answers. An integer holds whole numbers and a real holds numbers with fractional parts. A string would store the words true or false as text, but could not be used directly as a condition.
3

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

FeatureIntegerReal
Values storedWhole numbers onlyNumbers with a fractional part
Example42, −73.14, 1.62
Typical useCounting itemsMeasuring a height of 1.62 m
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

Exam tip

Pick the type that matches the meaning of the data. Counting people needs an integer, while measuring a height needs a real.

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 3 done

4

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 "Ada"\texttt{"Ada"}"Ada". Concatenation joins strings end to end, so "Hi " + "Ada"\texttt{"Hi " + "Ada"}"Hi " + "Ada" gives "Hi Ada"\texttt{"Hi Ada"}"Hi Ada".

Common mistake

A phone number should be a string, not an integer. You never add phone numbers, and a leading zero would be lost.

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.

Part 4 done

Quick check

One question on the part you just read

Easy

A value is set once at the start of a program and must never change. What should it be stored as?
Show answer and explanation

Answer: B · Worked explanation

A constant keeps one value for the whole program and cannot be changed by accident. A variable can be reassigned at any point, so it does not give that protection. String and Boolean are data types, not a way of fixing a value, so they do not answer the question.
5

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

Evaluate (17 mod 5) + (17 div 5) * 2\texttt{(17 mod 5) + (17 div 5) * 2}(17 mod 5) + (17 div 5) * 2.
  1. 1
    Work out the first bracket: 17 mod 5 is 2, since 17 = 3 × 5 + 2.
  2. 2
    Work out the second bracket: 17 div 5 is 3.
  3. 3
    Multiply: 3×2=63\times2=63×2=6.
  4. 4
    Add: 2+6=82+6=82+6=8.

Answer: The value of the expression is 8.

Comparisons such as age >= 13\texttt{age >= 13}age >= 13 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 5 done

6

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

Trace this program and state its output. total = 0; count = 3; total = total + count * 2; count = count - 1; output total, count\texttt{total = 0; count = 3; total = total + count * 2; count = count - 1; output total, count}total = 0; count = 3; total = total + count * 2; count = count - 1; output total, count
  1. 1
    Line 1 sets total to 0.
  2. 2
    Line 2 sets count to 3.
  3. 3
    Line 3 sets total to 0+3×2=60+3\times2=60+3×2=6.
  4. 4
    Line 4 sets count to 3−1=23-1=23−1=2.
  5. 5
    Line 5 outputs the current values of total and count.

Answer: The output is 6 and 2.

Trace table for the program

After linetotalcount
10empty
203
363
462
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

Exam tip

Write a new row for each line and copy unchanged values down. This stops you losing track of a variable.

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.

Note read

Check your recall[15 marks]

6 written questions. Answer each one in your head or on paper, then open the mark scheme.

  1. 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. 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. 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. 4.Calculate[2 marks]

    Calculate the value of 23 mod 4 + 23 div 4\texttt{23 mod 4 + 23 div 4}23 mod 4 + 23 div 4.

    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 3+5=83+5=83+5=8.

    • 1 mark for finding that 23 mod 4 is 3 and 23 div 4 is 5
    • 1 mark for adding to give 8
  5. 5.Determine[3 marks]

    Determine the output of this program. a = 4; b = 3; a = a - b; b = a + b * 2; output a, b\texttt{a = 4; b = 3; a = a - b; b = a + b * 2; output a, b}a = 4; b = 3; a = a - b; b = a + b * 2; output a, b

    Show mark schemeHide mark scheme

    Model answer

    a becomes 4−3=14-3=14−3=1. b becomes 1+3×2=71+3\times2=71+3×2=7. 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. 6.Explain[2 marks]

    Explain why x = x + 1\texttt{x = x + 1}x = x + 1 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.

3.

Easy·State[1 mark]

State which operator gives the remainder after whole-number division.
Show answer and explanation

Answer: C · Worked explanation

The mod operator returns the remainder, so 17 mod 5 is 2. The div operator returns the whole-number quotient, so 17 div 5 is 3. The / operator gives the full quotient, which can have a fractional part, and * multiplies and has nothing to do with remainders.
4.

Medium·Calculate[1 mark]

Calculate the value of 29 div 6\texttt{29 div 6}29 div 6.
Show answer and explanation

Answer: A · Worked explanation

The div operator gives the whole-number quotient. Since 6×4=246\times4=246×4=24 and 29 is less than 6×5=306\times5=306×5=30, the quotient is 4. The value 5 is the remainder, which is what 29 mod 6 gives. The value 4.83 is the result of ordinary division with /.
5.

Medium·Determine[1 mark]

Determine the output of this program. n = 6; n = n * 2 - 3; output n\texttt{n = 6; n = n * 2 - 3; output n}n = 6; n = n * 2 - 3; output n
Show answer and explanation

Answer: B · Worked explanation

Multiplication comes before subtraction, so n becomes 6×2−3=12−3=96\times2-3=12-3=96×2−3=12−3=9. The value −6 comes from subtracting first, as if the expression were 6×(2−3)6\times(2-3)6×(2−3). The value 12 forgets the subtraction, and 15 adds 3 instead of subtracting it.
6.

Medium[1 mark]

Which condition is true when age is from 13 to 19 inclusive?
Show answer and explanation

Answer: C · Worked explanation

Both tests must hold together, so AND joins them, and the inclusive limits need >= and <=. Joining the tests with OR makes the condition true for every number. Using > and < leaves out the ages 13 and 19. The last condition can never be true, because no age is both at most 13 and at least 19.
7.

Medium[1 mark]

A shop program stores the number of items in a basket and the average customer rating, 4.5. Which choice of data types is most suitable?
Show answer and explanation

Answer: C · Worked explanation

A count of items is a whole number, so it is an integer. A rating of 4.5 has a fractional part, so it needs a real. Storing both as integers would lose the 0.5. Reversing the types is wrong for the same reasons, and neither value is text or a true-or-false answer.
8.

Hard·Determine[1 mark]

Determine the output of this program. a = 2; b = 5; a = a + b; b = a - b; a = a - b; output a, b\texttt{a = 2; b = 5; a = a + b; b = a - b; a = a - b; output a, b}a = 2; b = 5; a = a + b; b = a - b; a = a - b; output a, b
Show answer and explanation

Answer: A · Worked explanation

Trace each line. a becomes 2+5=72+5=72+5=7. b becomes 7−5=27-5=27−5=2. a becomes 7−2=57-2=57−2=5. The output is 5 and 2, so the program swaps the two values. Tracing each line in turn is the safest method. The answer 2 and 5 assumes that nothing changed, and 7 and 2 stops tracing before the final assignment to a.
9.

Hard·Calculate[1 mark]

Calculate the value of (10 + 5) mod 4 + 3 * 2\texttt{(10 + 5) mod 4 + 3 * 2}(10 + 5) mod 4 + 3 * 2.
Show answer and explanation

Answer: C · Worked explanation

Brackets first: 10+5=1510+5=1510+5=15. Then 15 mod 4 is 3, and 3×2=63\times2=63×2=6. Adding gives 3+6=93+6=93+6=9. The value 17 ignores the brackets and works out 5 mod 4 on its own. The value 5 wrongly does the addition first, giving 15 mod (4+6)(4+6)(4+6), which is 15 mod 10, and 12 adds before multiplying.
10.

Hard·Deduce[1 mark]

Deduce for which values of x and y the condition (x > 5 AND y < 10) OR x == 0\texttt{(x > 5 AND y < 10) OR x == 0}(x > 5 AND y < 10) OR x == 0 is true.
Show answer and explanation

Answer: B · Worked explanation

When x is 0, the right-hand test x == 0 is true, so the whole OR is true whatever y is. When x is 7 and y is 12, y < 10 fails. When x is 3, x > 5 fails. When x is 6 and y is 10, y < 10 fails. In each of those cases neither side is true.

Keep going

Next topicB2.2 Data structuresPrevious topicB1.1 Approaches to computational thinking

More in B2 · Programming

  1. B2.1Programming fundamentalsNotes · 10 questions
  2. B2.2Data structuresNotes · 10 questions
  3. B2.3Programming constructsNotes · 10 questions
  4. B2.4Programming algorithmsNotes · 10 questions
  5. B2.5File processingNotes · 10 questions

All IB Computer Science topics →

Draft: written by Scribe and checked automatically, not yet by a subject reviewer.

Practice questions are original, written by Scribe and checked automatically for correctness.

Topic codes follow the official numbering. Not endorsed by IB Diploma Programme or any exam board.

On this page

0/6 read

  1. 1A variable stores a value that can change
  2. 2A constant keeps one value for the whole program
  3. 3Numbers are stored as integers or reals
  4. 4Booleans and strings store truth values and text
  5. 5Operators combine values to produce new values
  6. 6Tracing a program shows how its values change
  7. Check your recall
  8. Practice: 10 questions
All coursesScribeHow it worksPricingPrivacyTerms

Notes and questions are written by Scribe in our own words. Topic codes follow the official course numbering so you can find your place. Not produced or endorsed by the IB, College Board, AQA, OCR, Pearson Edexcel or any exam board.

© 2026 Scribe