Monday, April 7, 2025

7.13 Iteration (for and while) | Grade 9 New Curriculum Computer Science 2082

 


7.13 Iteration (for and while)

Iteration is the process of repeating a block of code multiple times until a specified condition is satisfied.

 

Types of iterations

·       for loop

·       while loop

 

For Loop

A for loop in Python is a control flow structure that allows to iterate over a sequence (like a list, tuple, string) or a range of numbers.

It is used when we know in advance how many times we want to execute a statement or a block of statements.

 

Syntax:

for variable in sequence:

    # Code to execute

 

Example:

# Iterating over a range of numbers from 0 to 4

for i in range(5):

    print(i)

 

Example:

#Using for loop to print “jump” five times

for x in range(5):

     print(“Jump!”)

 

Example:

# Iterating over a list

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:

    print(fruit)

 

While Loop

“While loop” in Python is a control structure that allows to repeatedly execute a block of code as long as certain conditions remain true.

Syntax:

while condition:

    # Code to execute

 

Example:

# Using while loop to print numbers from 0 to 4

i = 0

while i < 5:

    print(i)

    i += 1  # Incrementing the value of i to avoid infinite loop

 

pass Statement

·       In Python, the pass keyword is a null operation or a no-operation statement.

·       It acts as a placeholder where some code is required but no action is necessary.

·       It is often used when a statement is required by Python syntax, but you don’t want to execute any code.

 

Syntax:

if condition:

    # Some code here

else:

    pass # Nothing happens in the “else” case

 

Example:

for i in range(5):

    if i == 2:

        pass  # Do nothing if i is 2

    else:

        print(i)

 

Output:

0

1

3

4

 

In this example, when i is equal to 2, the pass statement is executed, which does nothing, so the loop continues without printing 2.

 

 

 

 

continue Statement

·       In Python, the continue keyword is used in loops (such as for or while loops).

·       It is used to skip the rest of the code inside the loop for the current iteration and move on to the next iteration.

·       It allows us to bypass certain parts of the loop based on a condition without exiting the loop entirely.

 

Example:

 

for i in range(5):

    if i == 2:

        continue  # Skip the rest of the code when i is 2

    print(i)

 

Output:

 

0

1

3

4

 

Here, when i == 2, the continue statement skips the print(i) and moves to the next iteration of the loop, so 2 is not printed.

 

break Statement

·       In Python, the break keyword is used in loops (such as for or while loops) to exit the loop early, even if the loop’s condition hasn’t been fully satisfied.

·       It allows us to terminate the loop based on a certain condition.

 

Example:

 

for i in range(5):

    if i == 3:

        break  # Exit the loop when i is 3

    print(i)

 

Output:

 

0

1

2

 

In this case, when i == 3, the break statement causes the loop to terminate, and no further numbers are printed.

 

1 comment:

  1. In Grade 9 Computer Science, iteration with for and while loops allows students to efficiently repeat tasks in their programs. This Atmosphere Switch in logic helps manage dynamic situations and automate repetitive processes.




    ReplyDelete