Search This Blog

While-else and for-else in Python:-


It will work when the condition of the loop and for loop will be false, suppose that we want to calculate result or we want to execute any other operation then we can use while--else and for--else.


Syntax of While---else Loop:-


init
while condition:
    statement
    increment
else:
   statement

Example:-
i=2
while i<2:
    print(i)
else:
    print('condition fail')

Syntax of for--else:-

for i in range(start,end):
    Statement
else:
   Statement

Example of for--else

sum=0

for i in range(1,10):
    print(i)
    sum=sum+i
else:
    print('Total is '+str(sum))





Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post