Break and Continue Examples

## Program 1

'''Program to show the use of break'''

'''Enter 10 numbers and find the total and average

If user enters any negative number or zero loop must end from that point

and show the total and average of all numbers entered so far...'''

 

t=0

i=1

count=0

while i<=10:

          n=int(input('Enter any number - '))

          if n<=0:

                    break

          t+=n

          i+=1

          count+=1

avg=t/count

print('Total Numbers entered are :',count)

print('Total=',t)

print('Average=',avg)

 

####################################################################

##Program 2

'''Program to show the use of Continue

'''Enter 10 numbers and find the total and average

If user enters any negative number or zero skip that value and take next input

At last show the total and average of all valid values entered'''

t=0

i=1

count=0

while i<=10:

          n=int(input('Enter any number - '))

          if n<=0:

                    i+=1

                    continue

          t+=n

          i+=1

          count+=1

avg=t/count

print('Total Numbers entered are :',count)

print('Total=',t)

print('Average=',avg)

No comments:

Post a Comment