ProgFund Lect Week 6
ProgFund Lect Week 6
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
3
for i in range(2, 4):
Practice Questions
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
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
9
User controlled Nested while
choice = 'y'
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
20
continue
sum = 0
number = 0
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