IB Computer Science HL · Year 2 · Lesson 5

Loops, functions, modularization and scope

Programming fundamentals turn algorithms into reliable executable procedures. Scope, data types, debugging and exceptions all affect correctness. Programming constructs control the order, branching and repetition of instructions and support modular solutions.

Paper 250 minutesB2.1.2 · B2.3.3 · B2.3.4
Today’s targets

What you need to be able to do

2 / 10
  • ConstructPrograms that can extract and manipulate substrings
  • ConstructPrograms that utilize looping structures to perform repeated actions
  • ConstructFunctions and modularization
B2.1.2B2.3.3B2.3.4
Paper 2 lensMatch the depth of every response to the command term. Previously learned content can move quickly, but retrieval must still be accurate.
Retrieve

Rapid Recall Deck

3 / 10

Say the answer aloud before flipping. Mark secure knowledge quickly and spend time on the gaps.

  • What is a substring?: A substring is a contiguous sequence of characters taken from within a larger string.
  • What kinds of operations can be performed when manipulating strings and substrings?: Identifying and extracting substrings, then altering, concatenating, or replacing text as required.
  • What is the difference between a counted loop and a conditional loop?: A counted loop repeats a known or controlled number of times; a conditional loop repeats while or until a condition determines that it should stop.
  • How can Boolean and relational expressions control a loop?: They determine whether the loop continues, stops, or whether conditional code inside the loop executes.
  • What is the purpose of a function?: A function packages a reusable block of code that can receive inputs and perform a defined task.
  • Why is modularization useful?: It divides a program into well-structured, reusable, maintainable parts that are easier to test and reason about.
B2.1.2 + B2.3.3 · Learn

Core knowledge and application

4 / 10
B2.1.2Construct

Programs that can extract and manipulate substrings

Programming fundamentals turn algorithms into reliable executable procedures. Scope, data types, debugging and exceptions all affect correctness.

Exam moveProduce the required code, diagram, query, model or representation accurately.
Required detail 1Writing of programs that accurately identify and extract substrings from given strings, demonstrating the ability to perform various manipulations, such as altering, concatenating or replacing
Assessment boundaryPaper 2

Explain it without notes

Construct: Programs that can extract and manipulate substrings in the context of a student-record processing program.

  • Writing of programs that accurately identify and extract substrings from given strings, demonstrating the ability to perform various manipulations, such as altering, concatenating or replacing
B2.3.3Construct

Programs that utilize looping structures to perform repeated actions

Programming constructs control the order, branching and repetition of instructions and support modular solutions.

Exam moveProduce the required code, diagram, query, model or representation accurately.
Required detail 1Types of loops, including counted loops and conditional loops, and appropriate use of each type
Required detail 2Conditional statements within loops, using Boolean and/or relational operators to govern the loop’s execution
Assessment boundaryPaper 2

Explain it without notes

Construct: Programs that utilize looping structures to perform repeated actions in the context of a program that validates and processes course choices.

  • Types of loops, including counted loops and conditional loops, and appropriate use of each type
  • Conditional statements within loops, using Boolean and/or relational operators to govern the loop’s execution
B2.3.4 · Learn

Core knowledge and application

5 / 10
B2.3.4Construct

Functions and modularization

Programming constructs control the order, branching and repetition of instructions and support modular solutions.

Exam moveProduce the required code, diagram, query, model or representation accurately.
Required detail 1Functions to define reusable blocks of code with different inputs
Required detail 2Modularization to create well-structured, reusable and maintainable code
Required detail 3The principles of scope (local versus global)
Required detail 4The benefits of code modularization, applying this concept to various programming scenarios
Assessment boundaryPaper 2

Explain it without notes

Construct: Functions and modularization in the context of a program that validates and processes course choices.

  • Functions to define reusable blocks of code with different inputs
  • Modularization to create well-structured, reusable and maintainable code
  • The principles of scope (local versus global)
  • The benefits of code modularization, applying this concept to various programming scenarios
Trace / construct

Programming checkpoint

6 / 10
def count_passes(scores):
    total = 0
    for score in scores:
        if score >= 50:
            total += 1
    return total
Before you run itTrace the code by hand. Identify state changes, branch/loop behaviour, and the final result.

Modify it

Change one condition, input or operation so the program solves a slightly different problem. Predict the effect before editing.

Worked example · B2.1.2

Substring extraction and manipulation

7 / 10

Python

course = "ComputerScience"
prefix = course[0:8]
updated = course.replace("Science", "Systems")
message = prefix + " / " + updated
print(message)
Trace itIdentify the extracted substring, the replacement, and the final concatenated string. Then change the code so it extracts Science instead.

Why modularize?

Put repeated string processing inside a function/method with inputs and a return value. This reduces duplicated code, makes testing easier and keeps local variables scoped to the task.

Apply

Transfer to a new scenario

8 / 10
ScenarioA student-record processing program needs a design or technical decision related to today’s topic. Explain what matters and why.
  • Writing of programs that accurately identify and extract substrings from given strings, demonstrating the ability to perform various manipulations, such as altering, concatenating or replacing
  • Types of loops, including counted loops and conditional loops, and appropriate use of each type
  • Conditional statements within loops, using Boolean and/or relational operators to govern the loop’s execution
  • Functions to define reusable blocks of code with different inputs
  • Modularization to create well-structured, reusable and maintainable code
Exam lens

Paper 2 practice

9 / 10
Build the response before checking notesUse precise terminology and match the required depth.
  1. Construct: Programs that can extract and manipulate substrings in the context of a student-record processing program.
  2. Construct: Programs that utilize looping structures to perform repeated actions in the context of a student-record processing program.
  3. Construct: Functions and modularization in the context of a student-record processing program.
Self-checkAnswer the exact command term. For explain, include mechanism/reason; for compare, pair criteria; for discuss/evaluate/justify, build supported reasoning and a conclusion.
Homework

Finish the learning cycle

10 / 10

IA — main task

Refine Criteria A/B/C. Use today’s lesson to make the problem specification, decomposition, algorithms or testing plan more precise where relevant.

Syllabus — 10–15 min

Repeat the recall deck and complete the lesson response prompt without model support.