Understand and use sequence, selection, and iteration

Resources | Subject Notes | Computer Science | Lesson Plan

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

Programming: Sequence, Selection, and Iteration

This section explores fundamental programming concepts: sequence, selection (conditional statements), 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 one after the other, from top to bottom.

  • The program starts at the first line of code.
  • Each line of code is executed in order.
  • Once a line of code is executed, the program moves to the next line.

Example:

  1. Start
  2. Input a number
  3. Check if the number is positive
  4. If positive, print "Positive"
  5. If not positive, print "Not positive"
  6. End

2. Selection (Conditional Statements)

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:


    number = 10
    if (number > 5) {
        print "Number is greater than 5"
    }
    

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:


    number = 3
    if (number > 5) {
        print "Number is greater than 5"
    } else {
        print "Number is not greater than 5"
    }
    

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 code blocks based on different possible outcomes.

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 repeat a block of code. It consists of a loop counter, an initial value, a condition, and an increment/decrement statement.

Syntax:

for (initialization; condition; increment/decrement) {
    // Code to be repeated
}

Example:


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

3.2 The while Loop

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

Syntax:

while (condition) {
    // Code to be repeated
}

Example:


    i = 1
    while (i <= 5) {
        print i
        i = i + 1
    }
    

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 before the condition is checked.

Syntax:

do {
    // Code to be repeated
} while (condition);

Example:


    i = 1
    do {
        print i
        i = i + 1
    } while (i <= 5);
    
Concept Description Example
Sequence Instructions executed in order. Input, process, output.
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 5.
Iteration (while loop) Repeating a block of code as long as a condition is true. Printing numbers from 1 to 5.