Introduction to Selection and Iteration

In computer science, we don’t just want our programs to run a list of instructions from top to bottom like a shopping list. We want them to be smart. To make a program "think" and "react," we use two fundamental concepts: Selection and Iteration. These are the building blocks of Theme B.2 Programming, and they allow us to control the "flow" of our code.

Selection is about making decisions (e.g., "If the password is correct, let the user in; otherwise, show an error.").
Iteration is about repeating actions (e.g., "Keep asking for the password until it is correct.").

Don't worry if these terms sound a bit technical! By the end of these notes, you'll see that you already use this logic every single day in real life.


1. Selection (Decision Making)

Selection allows a program to choose different paths of execution based on whether a condition is true or false. In the IB curriculum, you will use these structures in either Python or Java.

The "If" Statement

The most basic form of selection. If a condition is true, the code inside the block runs. If it is false, the computer simply skips it.

Example: If it is raining, take an umbrella.

The "If-Else" Statement

This provides an alternative path. It’s a "this or that" scenario.

Example: If your grade is \( \geq 50 \), you pass. Else, you fail.

The "If-Elif-Else" (Nested Selection)

Sometimes there are more than two possibilities. We use "Else If" (written as elif in Python or else if in Java) to check multiple conditions in order.

Example:
- If score \( > 80 \), grade is A.
- Else if score \( > 60 \), grade is B.
- Else, grade is C.

Quick Tip: The computer checks these from top to bottom. As soon as it finds a true condition, it runs that code and skips the rest of the "if" block!

Boolean Logic in Selection

Decisions are based on Boolean expressions—statements that are either TRUE or FALSE. You will use operators like:
- Comparison: \( == \) (equal to), \( != \) (not equal to), \( > \), \( < \), \( \geq \), \( \leq \).
- Logical: AND, OR, NOT.

Note: For more on these symbols, check the "Variables, data types and operators" chapter.

Key Takeaway: Selection is the "fork in the road" for your code. It ensures the program only does what is necessary under specific circumstances.


2. Iteration (Looping)

Iteration is the process of repeating a block of code. Instead of writing the same line 100 times, we use a loop to tell the computer: "Do this again!"

Count-Controlled Loops (The "For" Loop)

We use these when we know exactly how many times we want to repeat something.
Analogy: "Do 10 jumping jacks." (You know you start at 1 and stop at 10).

In programming, For loops are often used to go through Arrays or Collections (which you will learn about in a later chapter). For example, you might use a loop to print every name in a list of students.

Condition-Controlled Loops (The "While" Loop)

We use these when we don't know how many times we need to repeat, but we know when we should stop.
Analogy: "Keep scrubbing the floor while it is still dirty." (You don't know if it will take 5 minutes or 20 minutes, you just check the condition "is it dirty?").

Important Note: In a While loop, the condition is checked before the code runs. If the condition is false at the very beginning, the code inside the loop might never run at all!

The Infinite Loop (A Common Mistake!)

An infinite loop happens when the condition for a While loop never becomes false.
Example:
\( x = 1 \)
While \( x < 5 \):
    Print "Hello"
(Since \( x \) never changes, it will stay 1 forever, and "Hello" will print until the computer crashes! Always remember to update your variables inside the loop, like \( x = x + 1 \)).

Key Takeaway: Use For loops for a fixed number of repeats and While loops when you are waiting for a specific condition to change.


3. Choosing Between Selection and Iteration

Students often ask: "Which one do I use?" Look at the problem you are solving:

  • Does the program need to skip something? Use Selection.
  • Does the program need to repeat something? Use Iteration.
  • Does the program need to repeat something and make a decision inside? Use Both! (This is called nesting).

Did you know? Most modern software, from video games to social media apps, is just thousands of "If" statements and "Loops" working together. A video game "loop" runs 60 times every second to check if you pressed a button and then update the screen!


4. Summary Checklist

Before moving on to "Methods, parameters and modular code," make sure you understand:

  • If / Else / Elif: How to create different paths for your code.
  • Boolean Conditions: Using \( == \), \( > \), AND, and OR to define your decisions.
  • For Loops: Using a counter to repeat code a specific number of times.
  • While Loops: Repeating code based on a condition (and how to avoid infinite loops).

Quick Review:
- Selection = Choosing.
- Iteration = Repeating.
- Pre-test loop = A loop that checks the condition before it starts (like the While loop).

Don't worry if writing the syntax feels tricky at first! Whether you are using Java or Python, the logic remains the same. Focus on the "flow" first, and the semi-colons or indentations will become second nature with practice.