Every program stores and works with data. This unit covers what a variable is, the data types you need to know for IB CS, the difference between local and global variables, and the operators you use to calculate, compare and combine values. All of it is core B2.1 material, required at both SL and HL, so there is no HL-only content here. Examples follow the book's Java and Python style.
Slide 1 / 4
Variables and identifiers
A variable is a space in memory used to store data, referenced by a unique identifier (its name).
In Java you must declare the data type, for example int myNumber = 5; In Python you do not, for example myNumber = 5.
The = symbol assigns a value to a variable; it does not mean 'equals' in the maths sense.
Listing the variables you intend to use at the top of a program makes it easier for a human to read.
Check you've got it
0/4
1. Which statement about declaring variables in Java and Python is correct?
2. A program needs to store whether a student has paid a fee, which can only be yes or no. Which data type is most appropriate?
3. A variable x holds the value 100. Which of these expressions evaluates to True?
4. In an if statement, what does the operator && in Java (and 'and' in Python) require of two conditions?
Free revision content. Multiple-choice answers are marked instantly. Written answers are marked by AI on /free-mark.