Python While Loop


In this tutorial, we will explore the concept of loops in Python, specifically the ‘while’ loop.

A ‘while’ loop is used to execute a block of code repeatedly as long as a specified condition is true.

We will cover the syntax and usage of the ‘while’ loop in Python.

 

Basic while Loop Syntax


The basic syntax of a ‘while’ loop in Python is as follows:

while condition:
    # Code to be executed in each iteration


Here's an example that demonstrates the usage of a ‘while’ loop:

count = 0

while count < 5:
    print(count)
    count += 1


In the above example, the ‘while’ loop continues to execute as long as the ‘count’ variable is less than 5. The loop prints the value of ‘count’ in each iteration and increments it by 1.

 

Infinite while Loop


An infinite ‘while’ loop is a loop that continues to execute indefinitely unless a ‘break’ statement or another control flow mechanism is used to exit the loop.

Here's an example of an infinite ‘while’ loop:

while True:
    # Code to be executed indefinitely
    # ...


It is important to ensure that an exit condition is eventually met in an infinite loop to avoid an infinite execution.

 

‘break’ Statement


The ‘break’ statement is used to terminate a loop prematurely, even if the loop condition is still true. It is often used with an ‘if’ statement to check for a specific condition and exit the loop if that condition is met.

Here's an example:

count = 0

while True:
    print(count)
    count += 1
    if count >= 5:
        break


In the above example, the ‘while’ loop will continue indefinitely until the ‘count’ variable becomes greater than or equal to 5. When that condition is met, the ‘break’ statement is encountered, terminating the loop.

 

‘continue’ Statement


The ‘continue’ statement is used to skip the rest of the code in the current iteration and move on to the next iteration of the loop. It is often used with an ‘if’ statement to check for a specific condition and skip the current iteration if that condition is met.

Here's an example:

count = 0

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


In the above example, when the ‘count’ variable is equal to 3, the ‘continue’ statement is encountered. This skips the remaining code in the current iteration and moves on to the next iteration, without executing the ‘print(count)’ statement.

 

‘else’ Clause in a ‘while’ Loop


A ‘while’ loop can also have an optional ‘else’ clause that is executed when the loop condition becomes false. The code under the ‘else’ clause will execute once at the end of the loop, unless the loop is terminated by a ‘break’ statement.

Here's an example:

count = 0

while count < 5:
    print(count)
    count += 1
else:
    print("Loop completed successfully.")

In the above example, after the ‘while’ loop finishes executing (when ‘count’ becomes equal to 5), the message "Loop completed successfully" will be printed.

 

Nested ‘while’ Loops


You can nest one or more ‘while’ loops within another ‘while’ loop to create nested loops. This allows you to perform more complex iterations.

Here's an example:

row = 1

while row <= 3:
    col = 1
    while col <= 3:
        print(row, col)
        col += 1
    row += 1


In the above example, the outer ‘while’ loop controls the iteration over rows, while the nested ‘while’ loop controls the iteration over columns. This results in combinations like "(1, 1)," "(1, 2)," "(1, 3)," "(2, 1)," and so on.

 

Summary


In this tutorial, we covered the basics of the ‘while’ loop in Python. We learned how to use the ‘while’ loop to repeatedly execute a block of code as long as a condition is true. Additionally, we explored the ‘break’ and ‘continue’ statements for controlling the flow of the loop, and the ‘else’ clause for executing code when the loop condition becomes false. Furthermore, we looked at nested ‘while’ loops for more complex iterations. With the knowledge gained from this tutorial, you should be able to effectively use ‘while’ loops in your Python programs to perform repetitive tasks and control program flow based on conditions.
 

© 2022-2023 All rights reserved.