6 WhileLoop
6 WhileLoop
factor = 2
if n % factor == 0:
print(factor)
factor += 1
print (n)
num = 0
for num in range(10):
num = num + 1
if num == 5:
break
print('Num has value ' + str(num))
print('Encountered break!! Out of loop')
Output:
Num has value 1
Num has value 2
Num has value 3
Num has value 4
Encountered break!! Out of loop
When value of num becomes 5, the break statement is executed and the for loop
terminates.
Find the sum of all the positive numbers entered by the user. As soon as the user
enters a negative number, stop taking in any further input from user and display
the sum.
Write a Python program to check if the input number is prime or not.
If the loop’s condition is still true, the loop is entered again else the control is
transferred to the statement immediately following the loop.
num = 0
for num in range(6):
num = num + 1
if num == 3:
continue
print('Num has value ' + str(num))
print('End of loop')
Output:
Num has value 1
Num has value 2
Num has value 4
Num has value 5
Num has value 6
End of loop
Observe that the value3 is not printed in the output, but the loop continues after that
point to print other values before exiting the loop.
NESTED LOOPS
A loop may contain another loop inside it. A loop inside another loop is called a nested
loop.
Example
Output:
for b in range(4): Iteration 1 of outer loop
1
print( "Iteration " +str(b+1)+ " of outer loop")
2
for a in range(3): # nested loop 3
print(a+1) Out of inner loop
Iteration 2 of outer loop
print("Out of inner loop")
1
print("Out of outer loop") 2
3
Out of inner loop
Iteration 3 of outer loop
1
2
3
Out of inner loop
Iteration 4 of outer loop
1
2
3
Out of inner loop
Out of outer loop
Python does not impose any restriction on how many loops can be nested inside a loop or
on the levels of nesting. Also, a for loop may be nested inside a while loop, a while loop may
be nested inside a for loop, a while loop may be nested in a while loop or a for loop may be
nested in a for loop as in the above example.
Write a program to print the pattern for a number n input by the user, (here n = 5)
1
12
123
1234
12345
Solution:
num =int(input("Enter number of rows = "))
for i in range(1,num+1):
for j in range(1,i+1):
print(j, end ="")
print()
Output:
Enter number of rows = 5
1
12
123
1234
12345
Write a program to reverse a number input by the user
Output:
1456
6541
Write a program to sum the digits of a number input by the user
Output:
1456
16
For loop with else block
In Python we can have an optional ‘else’ block associated with the loop.
The ‘else’ block executes only when the loop has completed all the iterations.
Lets take an example:
Output:
0 1 2 3 4 The loop has completed execution
Note: The else block only executes when the loop is finished.
Infinite while loop
Example 1:
This will print the word ‘hello’ indefinitely because the condition will
always be true.
while True:
print("hello")
Example 2:
num = 1
while num<5:
print(num)
This will print ‘1’ indefinitely because inside loop we are not updating
the value of num, so the value of num will always remain 1 and the
condition num < 5 will always return true.
Nested while loop in Python
When a while loop is present inside another while loop then it is called nested
while loop. Lets take an example to understand this concept.
i = 1
j = 5
while i < 4:
while j < 8:
print(i, ",", j)
j = j + 1
i = i + 1
Output:
1 , 5
2 , 6
3 , 7
Python – while loop with else block
We can have a ‘else’ block associated with while loop. The ‘else’ block is
optional. It executes only after the loop finished execution.
num = 10
while num > 6:
print(num)
num = num-1
else:
print("loop is finished")
Output:
10 9 8 7
loop is finished