While-loops-Python

While Loop in Python

In programing language we use while loop to iterate over a block of code as long as the test expression (condition) is true. When the condition becomes false, the line immediately after the loop in the program is executed. Generally we use this loop when we don’t know the number of times to iterate beforehand.

In the while loop, condition is checked first. The body of the loop is entered only if the condition evaluates to True. After one iteration, the condition is checked again. This process continues until the condition evaluates to False. While loop falls under the category of indefinite iteration. Indefinite iteration means that the number of times the loop is executed isn’t specified explicitly in advance. In Python, the body of the while loop is determined through indentation. The body starts with indentation and the first un-indented line marks the end. Python interprets any non-zero value as True. None and 0 are interpreted as False. Consider the following example:

a=0
while a<6:
  print(f"Loop Iteration # {a}")
  a+=1

# The output will be :
Loop Iteration # 0
Loop Iteration # 1
Loop Iteration # 2
Loop Iteration # 3
Loop Iteration # 4
Loop Iteration # 5

Note : A loop becomes infinite loop if a condition never becomes False. You must use caution when using while loops because of the possibility that if condition never resolves to a False value, the loop will never ends. Such a loop are known an infinite loop.

1) Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

1.1 Continue Statement

Python Continue Statement returns the control to the beginning of the loop. With the continue statement we can stop the current iteration, and continue with the next. Consider the following code

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

#The Output of the code will be
1
2
4
5

1.2 Break Statement

Python Break Statement brings control out of the loop. With the break statement we can stop the loop even if the while condition is true. Consider the following example:

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

# The output of this loop will be
1
2
3

While Loop with Else

Same as with for loops, while loops can also have an optional else block. The else part is executed if the condition in the while loop evaluates to False. Hence, a while loop’s else part runs if no break occurs and the condition is false. Consider this example

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")

# The output of the code will be
Inside loop
Inside loop
Inside loop
Inside else

Note : The while loop can be terminated with a break statement. In such cases, the else part is not executed. Consider this code :

counter = 0

while counter < 5:
    print("Inside loop")
    counter = counter + 1
    if counter == 3:
        break
else:
    print("Inside else")

# The output of the code will be
Inside loop
Inside loop
Inside loop

Single statement while block

Just like the if block, if the while block consists of a single statement we can declare the entire loop in a single line. If there are multiple statements in the block that makes up the loop body, they can be separated by semicolons (;). Consider this example:

# Python program to illustrate 
# Single statement while block 
count = 0
while (count < 5): count += 1; print("Hello World")

# The output will be
Hello World
Hello World
Hello World
Hello World
Hello World

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *