0% found this document useful (0 votes)
162 views23 pages

Loop in Python (While Loop)

The document discusses while loops in Python. It provides the syntax for a basic while loop with initialization, condition, and increment/decrement. Several examples are given to demonstrate printing numbers, breaking/continuing loops, and nested loops. Practice problems are also provided to solidify understanding of while loops.

Uploaded by

ishan jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
162 views23 pages

Loop in Python (While Loop)

The document discusses while loops in Python. It provides the syntax for a basic while loop with initialization, condition, and increment/decrement. Several examples are given to demonstrate printing numbers, breaking/continuing loops, and nested loops. Practice problems are also provided to solidify understanding of while loops.

Uploaded by

ishan jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 23

While Loop :

Python uses the while loop to constitute a conditional


loop, by which repeated execution of a block of
statements is done until a condition is true.

While Loop Syntax:

<initialisation>
while <condition expression>:
statement1
statement2
<increment>
Ex 1:
num =1 //Initialization
while num<= 5: //Condition
print("num =", num)
num = num + 1 // increment

Output:
num = 1
num = 2
num = 3
num = 4
num = 5

Initialization , condition and increment are all compulsory


Ex 2:
WAP to print numbers from 1 to 10.
num =0 //Initialization
while num< 10: //Condition
num = num + 1 // increment
print("num =", num)

Output:
num = 1
num = 2
num = 3 …….will print till num=10
Same Ex 2 with a change :
WAP to print numbers from 1 to 10 with print
given before increment.
num =0 //Initialization
while num< 10: //Condition
print("num =", num)
num = num + 1 // increment

Output:
num = 0
num = 1
num = 2 …….will print till num=9
WAP to print even numbers from 1 to 10 .
n=0 //Initialization
while n< 10: //Condition
n=n+2 // increment
print("number =", n)
Output:
number = 2
number = 4
number = 6
number = 8
Number=10
Pr Prog. : WAP to print sum of even numbers till n.
WAP to print odd numbers from 1 to 10 .
num=1 //Initialization
while num< 10: //Condition
print("num =", num)
num = num + 2 // increment
Output:
num = 1
num = 3
num = 5
num = 7
num= 9
Pr Prog. : WAP to print sum of odd numbers till n.
Practice programs using while loop :

• WAP to generate a table of a number entered by a


user.

• WAP to print squares of numbers from 1 to 5 .

• WAP to print cubes of numbers from 1 to 5.

• WAP to print squares of numbers from 1 to n (n is user


entered) .

• WAP to print cubes of numbers from 1 to n (n is user


entered) .
The break Statement :
With the break statement we can stop the loop even
if the while condition is true.
Ex 3 :
WAP to Exit the loop when i is 3 :
i=1
while i < 6:
print(i)
if i == 3:
break
i=i+1
OUTPUT : 1 2 3 on newlines
The continue Statement :
With the continue statement we can stop the current
iteration, and continue with the next
Ex 4:
WAP to continue to the next iteration if i is 3 :
i=0
while i<6:
i=i+1
if i==3:
continue
print(i)

OUTPUT : 1 2 4 5
The else Statement :
With the else statement we can run a block of code
once when the condition no longer is true.
Ex 5 :
WAP to print a message once the condition is false.
i=1
while i<6:
print(i)
i=i+1
else:
print("i is no longer less than 6")

OUTPUT on next screen


OUTPUT of Ex 5:

1
2
3
4
5
i is no longer less than 6
WAP to add two numbers and print sum, till the
user enters ‘n’ as response using break statement.

res="y"
while res=="y":
a=int(input("Enter first number"))
b=int(input("Enter second number"))
print (a+b)
res=input("Want to continue")
if res=="n":
break
else:
continue
Reverse while Loop with (–)ve step :
The general syntax is :
<initialization>
while <condition> :
<statements>
<negative step>
Ex: i=10
while i>=0 :
print (i)
i=i - 1
Practice programs :
-WAP to print numbers in reverse order from 20 to 1.
-WAP to print numbers in reverse order from (n) to 1.
Nested while Loop :
A nested loop is a loop that occurs within another loop,
structurally similar to nested for statements.
Syntax :

<initialization 1 >
while <condition 1> : <first loop>
<initialization 2>:
while <condition 2> : <second inner loop>
<statements>
<increment 2>
<increment 1>
Practice programs :
-WAP to print the Fibonacci series (0,1,1,2,3,5..) for n term
(number of elements to be generated not the end limit).
Using For Loop Using While Loop

n=int(input("Enter number of a=0


b=1
terms to print"))
n=int(input("Number of terms you
a=0 need to generate"))
b=1
print(a,b,end=" ") print(a,end=' ')
for i in range(1,n+1): print(b,end=' ')
i=1
c=a+b
while i<=n :
print(c,end=" ") c=a+b
a=b print (c,end=' ')
b=c a=b
b=c
i=i+1
-WAP to check number entered is prime or not
(prime number gives zero twice only after division).
Using for Loop Using While Loop
cnt=0
count=0
n=int(input("Enter number to check "))
n=int(input("enter number to check"))
i=1
for i in range(1,n+1):
while i<=n :
if(n%i==0):
if n%i==0:
count=count+1
cnt=cnt+1
if count<=2:
i=i+1
print("Prime number")
if cnt<=2:
else:
print("Prime number ")
print("Not a Prime Number")
else:
print ("Not a prime number")
WAP to print sum of the series : 11 + 22 + 33 + 44 + 55
Using While Loop
Using For Loop s=0
i=1
sum=0
while i<=5 :
for i in range(1,6):
p=1
ans=1
j=1
for p in range(1,i+1):
while j<=i :
ans=ans*i
p=p*i
sum=sum+ans
j=j+1
print(sum)
print (i,"to the power",i,"is =",p)
s=s+p
i=i+1
print(end=' ')
print ("sum =",s)
Output will be :

1 to the power 1 is = 1
2 to the power 2 is = 4
3 to the power 3 is = 27
4 to the power 4 is = 256
5 to the power 5 is = 3125

Sum = 3413
WAP to print sum of the series : 11 + 22 + 33 + 44 …… nn

s=0
n=int(input("Enter the value of n"))
i=1
while i<=n :
p=1
j=1
while j<=i :
p=p*i
j=j+1
print (i,"to the power",i,"is =",p)
s=s+p
i=i+1
print(end=' ')
print ("sum =",s)
Output will be :

Enter the value of n 4


1 to the power 1 is = 1
2 to the power 2 is = 4
3 to the power 3 is = 27
4 to the power 4 is = 256
sum = 288
-WAP to print a pattern : 1
12
123
1234
12345
Using For Loop Using While Loop

i=1
while i<=5 :
for i in range (1,6):
j=1
for j in range(1,i+1):
while j<=i:
print(j, end=" ")
print (j,end=' ')
print(" ")
j=j+1
print("\n")
i=i+1
WAP to print sum of the series :

s=0
x=int(input("Enter value of x"))
n=int(input("Enter the value of n"))
i=1
while i<=n :
p=1
j=1
while j<=i :
p=p*x
j=j+1
print (x,"to the power",i,"is =",p)
s=s+(1/p)
i=i+1
print(end=' ')
print ("sum =",s)
Output will be :

Enter value of x 2
Enter the value of n 4
2 to the power 1 is = 2
2 to the power 2 is = 4
2 to the power 3 is = 8
2 to the power 4 is = 16
sum = 0.9375

You might also like