Ad Code

✨🎆 Diwali Dhamaka Offer! 🎆✨

Get 20% OFF on All Courses at Shiva Concept Solution click

While-else and for-else in Python

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

0 Comments