About the while loop

i didnt get while loop condtion steps

1 Like

@rameshwarpeharkar143

The syntax of while loop is:

while condition:
# body of while loop

Here,

A while loop evaluates the condition
If the condition evaluates to True, the code inside the while loop is executed.
condition is evaluated again.
This process continues until the condition is False.
When condition evaluates to False, the loop stops.

while loops are used when we don’t know the number of iterations and it ends when the condition is satisfied. for example,

a=0
while a<3:
    print(a)
    a=a+1

the above code would print
0
1
2

initially the value of a is 0. and while the value of a is less than 3, the loop will keep on going.
on the first iteration, value of a is 0, and its printed and on the next line a is increased by 1 which makes a=1
on the second iteration, value of a is 1, and its printed and on the next line a is increased by 1 which makes a=2
on the third iteration, value of a is 2, and its printed and on the next line a is increased by 1 which makes a=3
so, the condition is not satisfied anymore and it stops iterating.
its important we have something which would eventually end the condition else the loop would not end.