DoAssignment study guide

A2.2 · Sequence, Selection and Loops Explained

Learn to use sequence, selection, and repetition through clear examples and targeted practice.

Ontario Grade 11 Computer Science

Programming Concepts and Skills

Three ways to control the order of instructions in an ICS3U program

A program is a set of instructions a computer follows. Some instructions happen in order, some depend on a condition, and some happen repeatedly. These are sequence, selection, and repetition. This lesson uses Python 3; other classrooms may use different languages and syntax. The code is presented for tracing, not as code that has been run. As you read, follow the instructions and values yourself to work out the expected output.

What you will learn

  • Describe how sequence, selection, and repetition control which instructions a program carries out.
  • Trace simple Python 3 code and use its inputs and changing values to determine expected output.
  • Choose among sequence, selection, and repetition for a simple task.

Sequence: instructions in order

Sequence means carrying out instructions in the order they appear. For example, a program might receive a number, change it, and display the new value. Each instruction uses information available at that point. A later instruction does not happen before an earlier one.
Think in terms of input, process, and output. The input is information supplied to the program. The process uses or changes that information. The output is what the program displays or otherwise provides. In the first example, the input is text entered by the user, the process converts it to a whole number and adds 4, and the output is the resulting number.
In Python 3, input() provides text. The example uses int() to convert text containing a whole number into an integer. An assignment instruction stores a value in a variable. When tracing, record what each variable contains after its instruction; a later assignment can replace an earlier value. Python's parentheses and indentation are part of its syntax; a classroom using another language may write these instructions differently.
  • Instructions in a sequence are carried out in written order.
  • A variable stores a value that later instructions can use or replace.
  • Follow each instruction and update the trace before determining output.

Selection: choose a path using a condition

Selection lets a program choose which instruction or group of instructions to carry out. In Python 3, an if statement checks a condition. If the condition is true, the indented instructions beneath it are carried out. An else section, when present, gives the alternative path when the condition is false.
A comparison such as score >= 60 asks whether score is at least 60. Its result is true or false. Read the comparison carefully: a value equal to 60 satisfies “at least 60,” but would not satisfy “greater than 60.” In Python 3, >= means “greater than or equal to.”
Python 3 uses indentation to show which instructions belong to the if or else path. Only the selected path is carried out. After it finishes, the program continues with the next instruction after the selection. A flowchart can show this as a decision with two outcomes labelled Yes and No.
  • A condition determines which path is taken.
  • Check the comparison and its boundary value carefully.
  • In Python 3, indentation shows which instructions belong to each path.

Repetition: carry out instructions again

Repetition means carrying out a group of instructions more than once. A while loop repeats its indented instructions while its condition is true. The condition is checked before each pass. When the condition is false, the loop ends and the program continues after it.
A loop often uses a variable that changes during each pass. Increasing a count by one can move it toward a stopping point. Trace the condition and the changing value together: first check the condition, then trace the instructions in the body, including any update, and check again.
If a value used in the condition never changes in a way that can make the condition false, the loop may not end. In the example here, the count increases on every pass, so it eventually becomes greater than 3. Sequence, selection, and repetition can also be combined: a repeated section can contain an ordered set of instructions and a choice.
  • A while loop checks its condition before each pass.
  • Trace both the condition and any value changed during repetition.
  • The loop ends when its condition is false.

A method for tracing and choosing

Start by identifying the input, if there is one, and writing down its starting value. Follow instructions in order. At a selection, evaluate the condition using the current values and follow only the matching path. At a loop, check the condition; if it is true, trace one pass and update the values; then check again. Stop when the condition is false.
A trace table records changing values and helps explain how output is reached. Include the values that matter and use a row for each relevant pass or decision. The expected displayed output should agree with the traced values and the path taken. This is a hand trace, not a claim that the code was executed.
To choose a structure for a task, ask what it needs: ordered steps, a choice, repeated steps, or a combination. A task that adds a fixed amount once needs sequence. A task that chooses a message based on a condition needs selection. A task that displays several successive counts needs repetition. These structures describe the order in which instructions are carried out; a task may need more than one of them.
  • Start with the given inputs and trace in order.
  • Follow only the path selected by a condition.
  • For repetition, check the condition again after every pass.

Trace for the counting loop

Count before checkCondition count <= 3Displayed valueCount after the pass
1True12
2True23
3True34
4FalseNo pass4

Worked example

Sequence: add a fixed amount

A user enters 6. The program adds 4 and displays the result. Trace the input, the stored value, and the expected output.
Ordered calculation
StartRead amountAdd 4Display result

Read the input, calculate the result, then display it. The vertical spacing keeps each step distinct.

Python 3

amount = int(input())
result = amount + 4
print(result)
  1. Identify the input
    The user enters the text 6. The int() conversion stores the integer value 6 in amount.
  2. Follow the sequence
    The next instruction adds 4 to amount and stores the result in result. The final instruction displays result.
Answer: The expected displayed output is 10.
Check: Adding 4 to the input value 6 gives 10, in the order shown.

Worked example

Selection: choose a message

A program receives a whole-number score of 72. It displays “Meet target” when the score is at least 60, and “Keep practising” otherwise. Trace the condition and determine the output.
Choose a message
YesNoStartscore >= 60?Display Meet targetDisplay Keep practising

The condition selects exactly one of two messages. Yes and No show the two possible outcomes.

Python 3

score = 72
if score >= 60:
    print("Meet target")
else:
    print("Keep practising")
  1. Set the input value
    For this trace, score starts at 72. The input is a whole-number score.
  2. Evaluate the condition
    The condition asks whether 72 is at least 60. It is true, so the if instruction displays “Meet target.” The else instruction is not carried out.
Answer: The expected output is Meet target.
Check: Only the true branch is followed. A score of exactly 60 would also satisfy the condition because the comparison includes equality.

Worked example

Repetition: count up to a stopping value

A count starts at 1. The program displays the count and increases it by 1 while count is at most 3. Trace each pass and state what happens when the loop stops.
Count with a while loop
YesReturnNoSet count to 1count <= 3?Display countIncrease count by 1End

Yes follows the loop body and returns to the condition; No exits the loop.

Python 3

count = 1
while count <= 3:
    print(count)
    count = count + 1
  1. Check and repeat
    At count 1, the condition count <= 3 is true. The program displays 1 and increases count to 2. It checks again, displays 2, and increases count to 3. The condition is still true, so it displays 3 and increases count to 4.
  2. Stop when the condition is false
    The next check asks whether 4 is at most 3. It is false, so the loop ends. The value 4 is not displayed because the display instruction is inside the loop and the body does not run on this check.
Answer: The expected displayed values, in order, are 1, 2, and 3. When the loop ends, count is 4.
Check: The update increases count on every pass, so the condition that count is at most 3 eventually becomes false.

Common mistakes and how to avoid them

Assuming that an instruction later in the program happens before an earlier one.
Correction: Trace instructions in written order, updating variable values as each instruction is reached.
Treating an else section as if it also runs after the if section.
Correction: A selection carries out the instructions for the matching path only. If the condition is true, skip its else section.
Displaying the value that first makes a while condition false.
Correction: A while loop checks its condition before carrying out the body. If the condition is false, the body does not run.
Forgetting to update a value used by a loop condition.
Correction: Trace the update on each pass and check whether it can make the condition false.

Lesson summary

  • Sequence carries out instructions in their written order.
  • Selection uses a condition to choose which path to carry out.
  • Repetition carries out instructions again while a condition is true.
  • A careful trace records inputs, changing values, decisions, loop checks, and resulting output.

Check your understanding

Question 1

A program displays “Small” if size is less than 5 and displays “Large” otherwise. If size is 5, which message is displayed?
  1. Small
  2. Large
  3. Both messages
  4. Neither message
Show answer and explanation
Large
The condition size < 5 is false when size is 5, so the else path displays “Large.”

Question 2

A while loop displays value and then increases value by 1. It starts at 2 and repeats while value < 4. Which values are displayed?
  1. 2 and 3
  2. 2, 3, and 4
  3. 3 and 4
  4. 4 only
Show answer and explanation
2 and 3
The loop displays 2, increases to 3, then displays 3 and increases to 4. At 4, the condition is false, so the body does not run.

Question 3

Which control structure is needed when a program must carry out the same instructions several times?
  1. Sequence
  2. Selection
  3. Repetition
  4. Input
Show answer and explanation
Repetition
Repetition carries out a group of instructions more than once.

Key terms

Sequence
Instructions carried out in the order they appear.
Selection
A choice of instruction path based on whether a condition is true or false.
Repetition
Carrying out a group of instructions again, usually while a condition remains true.
Condition
A test whose result is true or false.
Trace
A step-by-step record of the instructions followed and the values that change.

Continue through ICS3U

View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation A2.2. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question