Skip to content
Learnearn.uk » Home » Indefinite Iteration

Indefinite Iteration

Indefinite Iteration

Indefinite Iteration

Indefinite iteration, also known as an indefinite loop, is used when you want to repeat a block of code until a certain condition is met. It involves iterating until a specific condition evaluates to true or false.

While Loop

While Loop

In Python Indefinite iteration is commonly implemented using “while” loops.

Here’s an example of indefinite iteration using a while loop in Python:

i = 0
while i < 5:
    print(i)
    i += 1

 

Output:

0
1 
2 
3 
4

In this example, the while loop continues to execute as long as the condition i < 5 is true. The value of i is incremented in each iteration until it reaches 5.

Break

While loop with break

You can use the break statement within a while loop to exit the loop prematurely, even if the condition is still true. This allows you to terminate the loop based on a certain condition or event.

Here’s an example of a while loop with a break statement:

i = 0

while i < 5:
    print(i)
    i += 1
    if i == 3:
        break

 

Output:

0
1
2

In this example, the while loop continues as long as the condition i < 5 is true. Inside the loop, the current value of i is printed, and then it is incremented by 1. The if statement checks if i is equal to 3. If the condition is true, the break statement is executed, and the loop is immediately terminated, regardless of whether the initial condition is still true.

As a result, the loop stops when i reaches the value of 3, and the program moves on to the next line of code after the loop.

The break statement can be useful when you want to exit a loop prematurely based on a certain condition or when you have accomplished a specific goal within the loop.

Continue Statement

Continue Statement

In Python, the continue statement is used within a loop, such as a while loop, to skip the rest of the current iteration and move on to the next iteration. It allows you to control the flow of the loop and bypass certain parts of the loop’s code block.

Here’s an example of a while loop with a continue statement:

i = 0

while i < 5:
    i += 1
    if i == 3:
        continue
    print(i)

 

Output:

0
1
2
4
5

The while loop continues as long as the condition i < 5 is true. Inside the loop, i is incremented by 1. The if statement checks if i is equal to 3. If the condition is true, the continue statement is executed, and the remaining code within the loop’s code block is skipped for that iteration. The loop then moves on to the next iteration.

As a result, when i is equal to 3, the print(i) statement is not executed for that iteration. The loop continues with the next iteration and prints the values of i for the other iterations.

The continue statement can be useful when you want to skip certain iterations of a loop based on a condition. It allows you to control the flow of the loop and selectively execute specific parts of the loop’s code block.

Resources