Programming Constructs (IB CS B2.3): Sequence, Selection, Loops and Functions

IB CS B2.3 explained: sequence, selection (if/else), the two kinds of loop (while vs for), and functions and modularization, with a worked example and exam tips.

In B2.1 you met the three building blocks of programming. B2.3 is where you actually build with them. This subtopic is about constructing working programs: putting instructions in the right order, choosing between paths, repeating actions, and packaging code into reusable functions. Get comfortable here and the practical parts of the course become a lot less scary. Let's break it down.

What does "sequence" mean in programming?

Sequence simply means the instructions run one after another, top to bottom, in the order you wrote them. It sounds obvious, but order is everything. Think of a recipe: cream the butter and sugar, then add the eggs, then fold in the flour. Swap two steps and you get a mess instead of a cake.

In code, a wrong order causes real bugs. If you try to use a total before you have added anything to it, or print a result before you have calculated it, the program does the wrong thing even though every individual line is correct. Careful sequencing also helps you avoid traps like an infinite loop, where the program never reaches its stopping point because the steps that would end it are in the wrong place or missing.

What is selection?

Selection is how a program makes decisions. A condition is tested, and the result, true or false, decides which block of code runs. The everyday version is the humble if statement: "if it is raining, take an umbrella."

There is a family of selection structures, and B2.3 expects you to know them. A plain if runs a block only when its condition is true. An if / else gives you two paths: one block when the condition is true, another when it is false. To handle several possibilities, you chain conditions with else if (written elif in Python). And when one variable is compared against many fixed values, a case or switch statement is often the tidiest choice. You can even place an if inside another if, which is called nesting.

What are the two kinds of loop?

Iteration, or looping, repeats a block of code so you do not have to write it out many times. The key distinction for the exam is between the two kinds.

A while loop is condition-controlled. It keeps repeating while a condition stays true and stops as soon as that condition becomes false. You reach for it when you do not know in advance how many repetitions you need, like "keep asking for a password until the user gets it right."

A for loop is count-controlled. It repeats a set number of times, which is perfect when you know the count up front, like "do this for each of the five questions." If you forget to change the condition inside a while loop, it never becomes false and you get an infinite loop, so always make sure something inside the loop moves you towards stopping.

What are functions and modularization?

As programs grow, writing everything in one long block becomes hard to read and harder to fix. Modularization is the practice of breaking a program into smaller named pieces. Each piece is usually a function (sometimes called a method or subprogram): a named block of code that does one job and can be called whenever you need it.

A function can take inputs, called parameters, and can hand back a result using return. The values you actually pass in when you call it are the arguments. Variables created inside a function are local, meaning they only exist while the function runs; variables declared outside any function are global and can be seen throughout the program.

Why bother? Functions make code easier to read, because a good name tells you what a block does at a glance. They make it easier to test, because you can check each function on its own. They make it easier to reuse, because the same function can be called from many places, and even from other programs through libraries. And they make it easier to update, because a change to the logic happens in one place.

A worked example: count the passing marks

Let's combine all four constructs in one small task: count how many students passed, where a pass is a mark of 50 or more.

First we write a function so the logic is reusable. Inside it we use sequence to set up a counter, iteration to visit every mark, and selection to test each one:




Now we call it with some data:

results = [40, 55, 60, 30]

results = [40, 55, 60, 30]

results = [40, 55, 60, 30]

Trace it through. The counter starts at 0. The loop checks 40 (no), 55 (yes, count becomes 1), 60 (yes, count becomes 2), and 30 (no). The function returns 2, which is then printed. One function, all three constructs, working together. That is exactly what B2.3 is asking you to do.

Common exam mistakes

A classic error is the infinite loop: a while loop whose condition never becomes false because nothing inside it changes the variable being tested. Always make sure each loop moves towards its stopping point.

Students often pick the wrong loop. If the number of repetitions is known, use a for loop; if it depends on a condition that might change, use a while loop. Choosing badly is not always wrong, but it can lose clarity marks.

Another slip is confusing parameters and arguments, or forgetting the return. A function that calculates a value but never returns it leaves the caller with nothing to use. If the question asks for a result, check that your function actually sends it back.

Finally, watch variable scope. Trying to read a local variable outside its function will fail, because it no longer exists once the function has finished. If a value is needed elsewhere, return it.

Quick recap

Sequence means instructions run in the order written; order errors cause bugs. Selection chooses a path using a condition: if, if/else, else if (elif), nested, and case. A while loop is condition-controlled (repeats while true); a for loop is count-controlled (repeats a set number of times). A function is a named, reusable block that takes parameters and can return a value. Modularization splits code into functions, making it easier to read, test, reuse, and update.

Frequently asked questions

What are the three programming constructs? The three constructs are sequence, selection, and iteration. Sequence runs instructions in order, selection chooses which block runs based on a condition, and iteration repeats a block. B2.3 also covers functions and modularization built from these.

What is the difference between a while loop and a for loop? A while loop is condition-controlled: it repeats while a condition stays true, which suits cases where you do not know the number of repetitions. A for loop is count-controlled: it repeats a set number of times, which suits cases where the count is known in advance.

What is the difference between a parameter and an argument? A parameter is the named input listed in a function's definition, like h, w, and d. An argument is the actual value you pass in when you call the function, like 2, 3, and 4. Parameters are the placeholders; arguments are the real values.

What does the return statement do? The return statement sends a value back from a function to the code that called it. Without it, a function may do work but hand back nothing, so the caller has no result to use or store.

What is the difference between local and global variables? A local variable is created inside a function and only exists while that function runs. A global variable is declared outside any function and can be accessed throughout the whole program. Trying to use a local variable outside its function will fail.

What is modularization and why is it useful? Modularization is breaking a program into smaller named functions or modules, each doing one job. It makes code easier to read, test independently, reuse in many places, and update, because a change happens in just one location.

Looking for a printable summary? Grab the Programming Constructs (IB CS B2.3): Sequence, Selection, Loops and Functions, 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 .

 

 

 

 

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com