Programming Fundamentals (IB CS B2.1): A Complete Guide
IB Computer Science B2.1 explained: variables and data types, operators, and the three programming constructs (sequence, selection, iteration). Examples and exam tips

This is where you start actually writing programs. Topic B2.1 covers the building blocks every program is made of: variables, data types, operators, and the three ways of controlling the flow. Master these and the rest of Theme B, from data structures to algorithms, builds directly on top.
This guide covers the B2.1 fundamentals: variables and data types, operators, the three programming constructs, and input and output.

What does IB CS topic B2.1 cover?
B2.1 covers the basic building blocks of programming: the data types and variables a program stores, the operators it uses, and the constructs that control the order in which statements run. It also covers reading input and producing output, so you can build small working programs.
What are variables and data types?
A variable is a named store for a value, and its data type decides what kind of value it holds and what you can do with it.

The data types you need are integer (whole numbers), real or float (decimals), string (text), boolean (true or false), and character (a single character). A variable can change as the program runs, while a constant is fixed once set. Putting a value into a variable is assignment, for example score = 0. Choosing the right type matters: you can do arithmetic on an integer, but "5" as a string is text, not a number.
What are operators?
Operators are the symbols that act on values, and they come in three families.

Arithmetic operators do maths: +, -, *, /, plus DIV for integer division (7 DIV 2 = 3) and MOD for the remainder (7 MOD 2 = 1). Relational operators compare two values and return true or false: equal, not equal, <, >, <=, >=. Logical operators combine conditions: AND (both true), OR (either true), and NOT (the opposite). Relational and logical operators are what power the conditions inside selection and loops.
What are the three programming constructs?
Every program, no matter how large, is built from just three ways of ordering statements.
Sequence runs statements one after another, in order. Selection uses a condition to choose which path runs, like an if / else. Iteration repeats a block of statements while a condition holds, which is a loop. Combine these three with variables and you can express any algorithm.
How do programs get input and produce output?
A program usually reads input from the user (or a file), processes it, and produces output. This is the input-process-output idea in action: input statements bring data in, the constructs and operators above process it, and output statements display the result. Validating input (checking it is sensible before using it) is part of writing robust programs.
Worked example: counting passing marks
Here is a small program that uses all three constructs to count how many marks are 50 or above.
The loop (iteration) visits each mark, the IF (selection) checks the condition with a relational operator, and the assignment inside runs in sequence. For marks of 40, 55, 60, and 30, count ends at 2.
Common exam mistakes for IB CS B2.1
Confusing assignment (storing a value,
score = 0) with comparison (testing equality in a condition).Mixing up data types, such as treating the string
"5"as the number5.Confusing selection (choose a path once) with iteration (repeat a block).
Getting DIV and MOD the wrong way round: DIV is the whole-number quotient, MOD is the remainder.
Off-by-one errors in loops, where the loop runs one time too many or too few.
Quick recap of B2.1
A variable stores a value; its data type is integer, real, string, boolean, or character.
A variable can change; a constant is fixed. Storing a value is assignment.
Operators: arithmetic (including DIV and MOD), relational, and logical.
The three constructs are sequence, selection, and iteration.
Programs follow input, process, output, and good ones validate their input.
Frequently asked questions
What is a variable in programming?
A variable is a named storage location that holds a value while a program runs. Its data type determines what kind of value it can hold, and you put a value into it using assignment, such as score = 0.
What is the difference between a variable and a constant?
A variable's value can change as the program runs, while a constant's value is fixed once it has been set. Constants are useful for values that should never change, such as the mathematical constant pi.
What are the three programming constructs?
The three constructs are sequence (statements run in order), selection (a condition chooses which path runs, like if/else), and iteration (a block repeats while a condition holds, a loop). Every program is built from these three.
What is the difference between selection and iteration?
Selection chooses which block of code runs once, based on a condition. Iteration repeats a block of code multiple times while a condition stays true. Selection is "which path?"; iteration is "how many times?".
What does MOD do in programming?
MOD gives the remainder after integer division. For example, 7 MOD 2 is 1, because 7 divided by 2 is 3 with a remainder of 1. It is often used to test whether a number is even or odd.
What is the difference between a string and an integer?
An integer is a whole number you can do arithmetic with, while a string is a sequence of characters treated as text. The string "5" is text, not the number 5, so you cannot do maths on it until it is converted.
Looking for a printable summary? Grab the B2.1 Shuttle Learning revision sheet, a three-page knowledge organiser covering everything above.
Looking for an IB Computer Science tutor?
Hi, I'm Yuness, the tutor behind Shuttle Learning. I work one to one with IB Computer Science students at SL and HL, and I deliberately take on only a handful each year so every student gets my full attention. Most go on to earn the 6s and 7s they were aiming for, in the final exams and the IA alike.
If you would like that kind of support, book a free 15-minute call and tell me what you are stuck on. You can press BOOK A LESSON .