While Loop In Python:

A while loop in Python is used to execute a block of code repeatedly until a certain condition is met. Here is the basic syntax of a while loop:

 

while condition:
    # code to be executed

The condition is checked at the beginning of each iteration. If it is True, the code inside the loop is executed. Afterward, the condition is checked again, and the loop continues until the condition becomes False.

Here is an example of a while loop that prints the numbers from 1 to 5:

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

 

In this example, the count variable is initialized to 1. The condition count <= 5 is checked at the beginning of each iteration. If it is True, the current value of count is printed to the console and count is incremented by 1. After 5 iterations, the value of count is 6, which is not less than or equal to 5, so the loop ends.

You can also use the break statement inside a while loop to terminate the loop prematurely. For example, the following code uses a while loop to repeatedly ask the user for input until they enter a non-empty string or type “quit”:

 

while True:
    user_input = input("Enter something (or type 'quit' to exit): ")
    if user_input == "":
        print("You didn't enter anything!")
    elif user_input == "quit":
        break
    else:
        print("You entered:", user_input)

 

In this example, the while loop runs indefinitely (True is always True). However, the break statement is used to exit the loop when the user types “quit”.

 

You can use the continue statement to skip the rest of the current iteration and move on to the next one. For example, the following code uses a while loop to print the numbers from 1 to 10, skipping the even numbers:

count = 1
while count <= 10:
    if count % 2 == 0:
        count += 1
        continue
    print(count)
    count += 1

 

In this example, the if statement checks if the current value of count is even. If it is, the continue statement is executed, which skips the rest of the current iteration (including the print statement) and moves on to the next iteration.

  • You can use the else clause to specify a block of code that should be executed when the loop terminates normally (i.e., when the condition becomes False). For example, the following code uses a while loop to find the smallest power of 2 that is greater than or equal to a given number:

 

num = 20
power = 1
while power < num:
    power *= 2
else:
    print("The smallest power of 2 that is greater than or equal to", num, "is", power)

 

In this example, the while loop multiplies power by 2 on each iteration until it is greater than or equal to num. When the loop terminates (because the condition power < num is no longer True), the else block is executed, which prints a message to the console.

  • You can use the while-else construct to combine the while loop and else clause into a single statement. The else block is executed when the condition becomes False, just like in the previous example. However, if you use the break statement to exit the loop prematurely, the else block will not be executed. For example, the following code uses a while-else loop to print the numbers from 1 to 5:
count = 1
while count <= 5:
    print(count)
    count += 1
else:
    print("Finished!")

 

In this example, the while loop prints the numbers from 1 to 5 and then terminates normally (because the condition count <= 5 is no longer True). The else block is then executed, which prints a message to the console.

Also Check: While loop exercises for CBSE Computer Science Students.

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved
error: Content is protected !!