Day16- While Loop in Python

Day16- While Loop in Python

while loop

While loop:

With the while loop we can execute a set of statements as long as a condition is true. As soon as the condition becomes False, the interpreter (python) comes out of the while loop.

There are three important things in while loop:

  • counter

  • logic

  • increment / decrement (if we don't write increment or decrement, it will create infinite loop. That is difficult to handle.)

Example 01:

counter = 0          # counter
while counter <= 5: # logic
    print(counter)
    counter += 1     #increment
else:
    print("Hello")

Output:

0
1
2
3
4
5
Hello

Example 02:

i = 0               # counter
while(i<3):         # logic
    print(i)
    i = i + 1  #increment #for every iteration i value increases+1

print("Done with the loop") #out of loop

Output:

0
1
2
Done with the loop

Example 03:

i = int(input("Enter the number: "))
while(i<=38):
    i = int(input("Enter the number: "))
    print(i)

print("Done with the loop")

Output:

Note: It takes input until user enter 38

Enter the number: 9
9
Enter the number: 34
34
Enter the number: 38
38
Done with the loop

Decrimenting While Loop:

Example 01:

count = 5             # counter
while (count > 0):    # logic
  print(count) 
  count = count - 1   # decrement

Output:

5
4
3
2
1

Note: Depending upon the while loop condition, we need to either increment or decrement the counter variable.

If the variable count/decrement logic is not written properly then it will make an infinite loop mean loop will continue forever until we don't stop it.

Else with While Loop:

We can even use the else statement with the while loop. Essentially as soon as the while loop condition becomes False, the interpreter comes out of the while loop and the else statement is executed.

Example 01:

count = 5            #logic
while (count > 0):   #counter
  print(count)
  count = count - 1  #decrement
else:                #else
    print("I am inside else")

Output:

5
4
3
2
1
I am inside else

Example 02:

x = 5
while (x > 0):
    print(x)
    x = x - 1
else:
    print('counter is 0')

Output:

5
4
3
2
1
counter is 0

Quiz Questions:

  • Print alphabets from a to g using while loop using increment.

  • Print alphabets from a to g using while loop using decrement.

  • Print year from 1980 to 2023 using while loop.


Emurate Do-While loop in python:

do..while is a loop in which a set of instructions will execute at least once (irrespective of the condition) and then the repetition of loop's body will depend on the condition passed at the end of the while loop.

  • It is also known as an exit-controlled loop.

  • Do while loop - only in C/C++ and Java. Not in python.

  • We use break statement in do while loop.

How it works:

  • First execute - Donot check whether condition is true or false.

  • Then, second time - Check whether condition is true or false.

  • Then, third time - Executes only if the condition is true in second time or else came out of loop.

How to emulate do while loop in python?

To create a do while loop in Python, you need to modify the while loop a bit in order to get similar behavior to a do while loop.

Syntax:

do {
    #loop body;
} while(condition)

Example 01:

i = 0
while True:
  print(i)
  i = i +1
  if(i%10 == 0):
   break

Output:

0
1
2
3
4
5
6
7
8
9

Example 02:

while True:
  number = int(input("Enter a positive number: "))
  print(number)
  if not number > 0:
    break

Output:

Enter a positive number: 1
1
Enter a positive number: 4
4
Enter a positive number: -1
-1

Explanation:

This loop uses True as its formal condition. This trick turns the loop into an infinite loop. Before the conditional statement, the loop runs all the required processing and updates the breaking condition. If this condition evaluates to true, then the break statement breaks out of the loop, and the program execution continues its normal path.