0 results for
ICT
Computing
Responsive image

C5 - Control Structures

Overview
Flight Path
Learning Objectives
0 results forGuest
GradeSkill_name
 3.5 I can select, apply, use and interpret FOR Loops to analyse and improve the effectiveness of code.
 3.5 I can select, apply, use and interpret REPEAT Loops to analyse and improve the effectiveness of code.
 3.5 I can select, apply, use and interpret WHILE Loops to analyse and improve the effectiveness of code.
 3.5 I can select, apply, use and interpret BREAK to analyse and improve the effectiveness of code.
 3.5 I can select, apply, use and interpret IF Branches to analyse and improve the effectiveness of code.
 3.5 I can select, apply, use and interpret THEN ELSE Branches to analyse and improve the effectiveness of code.
 3.5 I can select, apply, use and interpret ELSEIF (ELIF) Branches to analyse and improve the effectiveness of code.
 3.5 I can select, apply, use and define Functions to analyse and improve the effectiveness of code.
 3.5 I can select, apply, use and interpret Function arguments to analyse and improve the effectiveness of code.

Strength

Improvement

Targets

Progress this topic

0%


Overall Progress for this unit

NAN%


LO60 :- I can select, apply, use and interpret FOR Loops to analyse and improve the effectiveness of code.

For Loop

In computer science a for-loop (or simply for loop) is a control flow statement for specifying iteration, which allows code to be executed repeatedly.

Click on the image on the right to see more.

Here is an example in Python

# range(1, 6) gives values from 1 to 5 inclusive (but not 6)
for counter in range(1, 6):
# statements

Flight Path

Equivalence with while-loops

A for-loop can be converted into an equivalent while-loop by incrementing a counter variable directly. The following pseudocode illustrates this technique:

factorial := 1
  for counter from 1 to 5
    factorial := factorial * counter

is easily translated into the following while-loop:

factorial := 1
counter := 1
while counter <= 5
    factorial := factorial * counter
    counter := counter + 1

Task

Convert the above psuedocode into python code for both the 'for' and 'while' loop.

Test yourself

LO61 :- I can select, apply, use and interpret REPEAT Loops to analyse and improve the effectiveness of code.

Repeat Loop

(Or "do while loop") A loop construct found in many procedural languages which repeatedly executes some instructions while a condition is true. Repeat loops are found in Perl, Pascal, BASIC and C. The initial keyword may be "repeat" or "do" and the condition may be introduced with a "while" or "until" keyword..

Click on the image on the right to see more.

Flight Path

Example of a Repeat loop in Pascal

Repeat

Test yourself

LO62 :- I can select, apply, use and interpret WHILE Loops to analyse and improve the effectiveness of code.

While loops

In while loop, test expression is checked first. The body of the loop is entered only if the test_expression evaluates to True. After one iteration, the test expression is checked again. This process continues until the test_expression evaluates to False.

In Python, the body of the while loop is determined through indentation. Body starts with indentation and the first unindented line marks the end. Python interprets any non-zero value as True. None and 0 are interpreted as False.

Code example in Python

# checks if condition counter is less than 10 is True
while counter < 10:
# statements

Flight Path

Practical Tasks

The following docode illustrates a simple while loop:

# Program to add natural numbers up to # sum = 1+2+3+...+n
# To take input from the user,

n = int(input("Enter n: "))

# initialize sum and counter
sum = 0
i = 1
while i <= n:
  sum = sum + i
  i = i+1 # update counter
# print the sum
print("The sum is", sum)

Task

Recreate the above python code as a for loop.

Test yourself

LO63 :- I can select, apply, use and interpret BREAK to analyse and improve the effectiveness of code.

Break and Continue

In Python, break and continue statements can alter the flow of a normal loop.Loops iterate over a block of code until test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without cheking test expression.

The break and continue statements are used in these cases.

Python break statement

The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop.

Continue

The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration. break

Break

Flight Path

Continue

break
Code examples in Python

# Use of break statement inside loop
for val in "string":
  if val == "i":
    break
  print(val)
print("The end")

# Program to show the use of continue statement inside loops
for val in "string":
  if val == "i":
    continue
  print(val)
print("The end")

Task

Recreate the above python code.

LO64 :- I can select, apply, use and interpret IF Branches to analyse and improve the effectiveness of code.

If Statement

Flight Path
Flight Path

Decision making is required when we want to execute a code only if a certain condition is satisfied.

The if…elif…else statement is used in Python for decision making.

Python if Statement Syntax

if test expression:
     statement(s)

Here, the program evaluates the test expression and will execute statement(s) only if the text expression is True.

If the test expression is False, the statement(s) is not executed.

In Python, the body of the if statement is indicated by the indentation. Body starts with an indentation and the first unindented line marks the end.

Python interprets non-zero values as True. None and 0 are interpreted as False.
Code example in Python
# If the number is positive, we print an appropriate message
num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")

num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")

Test yourself

LO65 :- I can select, apply, use and interpret THEN ELSE Branches to analyse and improve the effectiveness of code.

if ... THEN ... else

The THEN statement when coding is usually implied rather than stated. If a statement condition is True THEN we apply the code connected with that condiotion

Python THEN Statement Syntax

Flight Path
if test expression(True):
   THEN do statement(s)

if ... then ... ELSE

Flight Path

The ELSE statement when coding is usually is what we do if the condition we test is False. If a condition is True THEN we apply the code connected with that condition ELSE if it is False then we apply some other code statements

Python THEN Statement Syntax

if test expression(True):
   THEN do statement(s)
ELSE(False):
    do statement(s)

Test yourself

LO66 :- I can select, apply, use and interpret ELSEIF (ELIF) Branches to analyse and improve the effectiveness of code.

if ... THEN ... else

The THEN statement when coding is usually implied rather than stated. If a statement condition is True THEN we apply the code connected with that condiotion

Python THEN Statement Syntax

Flight Path
if test expression(True):
   THEN do statement(s)

LO67 :- I can select, apply, use and define Functions to analyse and improve the effectiveness of code.

if ... then ... ELSE

Flight Path

The ELSE statement when coding is usually is what we do if the condition we test is False. If a condition is True THEN we apply the code connected with that condition ELSE if it is False then we apply some other code statements

Python THEN Statement Syntax

if test expression(True):
   THEN do statement(s)
ELSE(False):
    do statement(s)

LO68 :- I can select, apply, use and interpret Function arguments to analyse and improve the effectiveness of code.

Break and Continue

In Python, break and continue statements can alter the flow of a normal loop.Loops iterate over a block of code until test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without cheking test expression.

The break and continue statements are used in these cases.

Python break statement

The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop.

Continue

The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration. break

Break

Flight Path

Continue

break
Code examples in Python

# Use of break statement inside loop
for val in "string":
  if val == "i":
    break
  print(val)
print("The end")

# Program to show the use of continue statement inside loops
for val in "string":
  if val == "i":
    continue
  print(val)
print("The end")

Task

Recreate the above python code.


0 results forGuest
Recent Comments

Teacher Date: 2025-10-26


Guest