Understand and use sequence, selection, and iteration

Resources | Subject Notes | Computer Science

IGCSE Computer Science - Programming: Sequence, Selection, and Iteration

Programming: Sequence, Selection, and Iteration

This section explores the fundamental concepts of programming: sequence, selection (conditional branching), and iteration (loops). Understanding these concepts is crucial for writing effective and logical computer programs.

1. Sequence

Sequence refers to the order in which instructions are executed in a program. Instructions are typically executed line by line, from top to bottom.

  • The program starts at the first line.
  • Each instruction is executed in the order it appears.
  • Once an instruction is executed, the next one begins.

Example:

  1. Start
  2. Display "Hello, World!"
  3. Stop

2. Selection (Conditional Branching)

Selection statements allow a program to make decisions and execute different blocks of code based on whether a condition is true or false. The primary selection statement is the if statement.

2.1 The if Statement

The if statement checks a condition. If the condition is true, the code block within the if statement is executed. If the condition is false, the code block is skipped.

Syntax:

if (condition) {
    // Code to execute if the condition is true
}

Example:

if (age >= 18) { print("You are an adult."); }

2.2 The else Statement

The else statement provides an alternative block of code to execute if the condition in the if statement is false.

Syntax:

if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}

Example:

if (score > 50) { print("Passed"); } else { print("Failed"); }

2.3 The else if Statement

The else if statement allows you to check multiple conditions in sequence. It's useful when you need to execute different blocks of code based on different sets of conditions.

Syntax:

if (condition1) {
    // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition2 is true
} else {
    // Code to execute if none of the conditions are true
}

3. Iteration (Loops)

Iteration allows a block of code to be executed repeatedly. There are three main types of loops: for, while, and do-while.

3.1 The for Loop

The for loop is used when you know in advance how many times you want to execute a block of code. It typically includes an initialization, a condition, and an increment/decrement statement.

Syntax:

for (initialization; condition; increment/decrement) {
    // Code to execute repeatedly
}

Example:

for (i = 1; i <= 10; i++) { print(i); }

3.2 The while Loop

The while loop is used when you want to execute a block of code as long as a certain condition is true. The condition is checked at the beginning of each iteration.

Syntax:

while (condition) {
    // Code to execute repeatedly
}

Example:

i = 1; while (i <= 10) { print(i); i++; }

3.3 The do-while Loop

The do-while loop is similar to the while loop, but the code block is executed at least once, regardless of the initial condition.

Syntax:

do {
    // Code to execute repeatedly
} while (condition);

Example:

i = 1; do { print(i); i++; } while (i <= 10);

Concept Description Example
Sequence Instructions are executed in order. Displaying a series of messages.
Selection (if/else) Executing different code blocks based on conditions. Checking if a number is positive or negative.
Iteration (for loop) Repeating a block of code a specific number of times. Printing numbers from 1 to 10.
Iteration (while loop) Repeating a block of code as long as a condition is true. Repeating a task until a user enters a specific input.