While Loop in Python

Loop is used to repeat the iterations upto the given condition is true.

While loop is shown here to display the numbers from 1 to 100

i=1
while i<=100:
print(i)
i=i+1

    Output:
   1 2 3………………………………………100

Neting of the loop can also be done
Following program prints the table of all numbers 1-5 in this case

n=1
while n<=5:
i=1
print("table for ",n)
while i<=10:
print(n*i, end=" ")
i=i+1
n=n+1
print() # to come to next line after table print

break statement may be used to explicitly break the loop .
By using break the loops breaks even if the loop condition is true.

Program to find prime number can be implemented for break statement.

n=int(input("enter the number to check"))
i=2
flag=0
while i<n:
if n%i==0:
flag=1
break
i=i+1
if flag==0:
print("number is prime")
else:
print("number is not prime")
Click Here

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial