FortyFive › Revision notes › IB Computer Science › IB Computer Science: B2 · Programming
IB Computer Science: B2 · Programming Data types, control structures, files, searching and sorting. 5 subtopics, 21 key terms.
In this topic: B2.1 · B2.2 · B2.3 · B2.4 · B2.5
B2.1 Programming fundamentalsKey terms Variable A named storage location whose value can change. Data type The kind of data a variable holds: integer, float, string, Boolean. Selection Choosing a path with if / else if / else. Iteration Repeating code: count-controlled (for) or condition-controlled (while). Exception An error at run time that can be handled (try / except). Operators: arithmetic (+ − * / // % **), comparison (== != < > <= >=), logical (and, or, not). // is integer division and % is the remainder.
The full notes, worked example and check questions for this subtopic are in the app.
Exam tip: Know the difference between = (assignment) and == (comparison): a classic error to spot in code questions.
B2.2 Data structures: arrays and listsKey terms Array A fixed-size collection of elements of the same type, accessed by index. List A dynamic collection that can grow and shrink (Python list, Java ArrayList). 2D array An array of arrays, like a table with rows and columns. Index The position of an element; usually starting at 0. Static structures have a fixed size (fast, memory-efficient); dynamic ones resize (flexible, some overhead).
The full notes, worked example and check questions for this subtopic are in the app.
Exam tip: When looping with an index, remember the last valid index is len − 1.
B2.3 Modular programming and recursionKey terms Function / method A named block of code that performs a task and can return a value. Parameter / argument A variable in a function definition / the value passed in when it is called. Scope Where a variable can be accessed: local (inside a function) or global. Recursion A function that calls itself, with a base case to stop. Benefits of modules: reuse, easier testing and debugging, teamwork, readability, abstraction.
The full notes, worked example and check questions for this subtopic are in the app.
Exam tip: For recursion questions, write each call on a new line, then unwind from the base case upwards.
B2.4 Searching, sorting and efficiencyKey terms Linear search Checks each element in turn; works on unsorted data; O(n). Binary search Repeatedly halves a sorted list; O(log n). Bubble sort Repeatedly swaps adjacent elements that are out of order; O(n²). Selection sort Repeatedly selects the smallest remaining element and moves it into place; O(n²). Big O notation Describes how running time grows with input size n in the worst case. Binary search on 1 million sorted items needs at most about 20 comparisons (\(2^{20}\approx10^{6}\)); linear search may need 1 million.
The full notes, worked example, interactive diagram and check questions for this subtopic are in the app.
Exam tip: Binary search only works on sorted data: say so whenever you choose it.
B2.5 File processingKey terms File mode How a file is opened: read ('r'), write ('w', overwrites), append ('a'). Text file Stores characters, often one record per line (e.g. CSV). Persistence Keeping data after the program ends. Steps: open → read or write → close (using with closes automatically).
The full notes, worked example and check questions for this subtopic are in the app.
Exam tip: Always strip the newline character from lines read from a file before converting them.
← B1 · Computational thinking · B3 · Object-oriented programming →
← All IB Computer Science topics
All notes, examples and questions are written by FortyFive. FortyFive is an independent resource, not affiliated with or endorsed by the International Baccalaureate Organization. “IB” and “International Baccalaureate” are trademarks of the International Baccalaureate Organization.