Computer Science · Theme B: Computational thinking and problem-solving
B2.3 — Programming constructs
Computer Science · SL / HL · syllabus-mapped notes
CS_B2.3.1
Correct sequence of instructions
How instruction order affects output, and avoiding infinite loops, deadlock and wrong output.
Full note — freeSequence is the order in which instructions run. You must be able to construct programs whose steps are in the correct order to meet the objective. Impact of instruction order A program executes top to bottom, so a value must be set before it is used. Reordering steps can change or break the result. e.g. total = 0 total = total + 5 # correct: total defined first print(total) # 5 # swapping lines 1 and 2 would cause an error Errors to avoid Infinite loop : a loop whose condition never becomes false, so it never ends (forgetting to update the counter). Deadlock : two tasks each wait for the other, so neither proceeds. Incorrect output : logically valid code that produces the wrong answer, often from mis-ordered steps. In every loop, make sure the value in the condition is actually changed inside the loop, or it will run forever.
CS_B2.3.2
Selection structures
if, else, else if / elif with Boolean (AND, OR, NOT) and relational operators.
Selection runs different code depending on a condition. You must be able to construct programs using selection structures. Keywords (must include) Python : if, elif, else. Java : if, else if, else. Each guards a block that runs only when its condition is true. Relational …
CS_B2.3.3
Looping structures
Counted and conditional loops, and choosing the right one.
Loops repeat a block of code. You must be able to construct programs that use looping structures, choosing the appropriate type. Counted loop Repeats a known number of times. Use when you know how many iterations are needed. e.g. for i in range(5): # runs …
CS_B2.3.4
Functions and modularization
Reusable functions, modular design, scope and the benefits of both.
You must be able to construct functions and apply modularization . Functions A named, reusable block of code that takes inputs (parameters) and can return a result. Call it many times with different inputs instead of repeating code. e.g. def area(w, h): # reusable block …
