Programming Fundamentals
Department of CS&IT
Nested Loop
Nested Loops
Output:
i= 1 j=
1
🞂 Nested loop: loop that is contained i= 1 j=
inside another loop. 2
i= 1 j=
🞂 Inner loop goes through all of its
3
iterations for each iteration of outer i= 2 j=
loop. 1
🞂 Inner loops complete their i= 2 j=
iterations faster than outer loops. 2
i= 2 j=
🞂 Total number of iterations in nested 3
loop: number_iterations_outer x i= 3 j=
1
number_iterations_inner Number
i= 3 j= of Iterations = 4 x
3
2=
for i in range(1,5): 12
i= 3 j=
3
for j in range(1,4): i= 4 j=
print("i=",i," 1
i= 4 j=
2
j=",j) 2
i= 4 j=
Nested for Loops
🞂 Nested loop: loop that is contained
inside another loop
Practice Questions
🞂 Problem:Write a program that uses nested
for loops to print a multiple tables from (2 –
10).
3
for i in range(2, 4):
# Printing inside the outer loop
# Running inner loop from 1 to 10
for j in range(1, 11):
# Printing inside the inner loop
print(i, "*", j, "=", i*j)
# Printing inside the outer loop
print()
Nested for Loops
Practice Questions
🞂 Problem:Writea program that uses nested
for loops to print a Factorial result from (2 –
10).
5
Nested while Loops
i=1
while i <4:
j=1
while j < 3:
print("i=",i,"j=",j)
j +=1
i+=1
i -= 1
j -= 1
print("Total Iterations
= ", (i*j))
RQ 12
Nested while Loops
Practice Questions
🞂 Problem:Writea program that uses
nested while loops to print a multiple
tables from (2 – 10).
7
i=2
while i<=10:
j=1
while j<=12:
print (i , "*", j , "=" , i*j)
j+=1
i+=1
print ("\n")
Nested for Loops
Practice Questions
🞂 Problem:Write a program that uses
nested while loops to print a Factorial
result from (2 – 10).
9
User controlled Nested while
choice = 'y'
while (choice == 'y' or choice=='Y'):
tno = int(input("Enter Table No to print =
")) times=1
while (times<11):
print(tno,"x",times,"=",(tno*times))
times+=1
choice = input("Do you want to print
Another Table (Y/N) = ")
print("Program Complete")
RQ 10
User controlled Nested while
RQ 11
break Statement in Python
🞂 Thebreak statement terminates the loop
containing it. Control of the program flows to
the statement immediately after the body of the
loop.
🞂 In
nested Loops, if break statement is inside a
inner loop, it will terminate the inner most
loop.
🞂 If
break statement is inside a outer loop, it
will terminate both inner13 and outer loop.
break Statement in Python
# Use of break statement
# inside loop
for val in "string":
if val == "i":
break
print(val)
print("The
end")
s t
r
Th
e
14
e
Python break statement
for letter in # First
'Python': if letter Example
== 'h':
break
print('Current Letter :',
letter)
var = 10 # Second
while var > Example
0:
var = var -
1 if var ==
5:
print('Current variable value :',
var)break
print("Program Complete!")
RQ 19
Python continue statement
🞂 The
continue statement is used to skip
the rest of the code inside a loop for
the current iteration only.
🞂 Loop
does not terminate but continues
on with the next iteration.
16
Python continue statement
# Program to show the use
of # continue statement
inside
# loops
for val in "string":
if val == "i":
continue
print(val)
print("The end")
s
t
r
n
g
17
T
h
Python continue statement
for letter in # First
'Python': if letter Example
== 'h':
continue
print('Current Letter :',
letter)
var = 10 # Second
while var > Example
0:
var = var -
1 if var ==
5:
print('Current variable value :',
var)continue
print("Program Complete!")
RQ 18
Python continue statement
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value :
9 Current variable value
: 8 Current variable
value : 7 Current
variable value : 6
Current variable value :
4 Current variable value
: 3 Current variable
value : 2 Current
variable value : 1
Current variable value :
RQ 19
0 Program Complete!
break
sum = 0
number = 0
while number < 20:
number += 1
sum += number
if sum >= 100:
Break out of break
the loop
print("The number is ", number)
print("The sum is ", sum)
20
continue
sum = 0
number = 0
while (number <
20): number +=
Jump to the i
1f (number == 10 or number ==
end of the 11):continue
iteration sum +=
number
print("The sum is ",
sum)
21
Post-test Loop with break
// C language post-test (do-while) # Python
// loop
int i = 1;
i=1
do{ while
printf("%d\n", i);
i = i + 1;
True:
} while(i <= 3); print(i)
Output: i=i+
1
2 1 if(i >
3
3):
22
break