ProgFund Lect Week 6
ProgFund Lect Week 6
(SWE – 102)
3
Nested if
Syntax
The syntax of the nested if...elif...else construct
may be −
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
elif expression4:
statement(s)
else:
statement(s)
else:
statement(s)
RQ 4
Nested if
var = 100
if (var < 200):
print("Value is less than 200")
if(var == 150):
print("Value is 150")
elif(var == 100):
print("Value is 100")
elif(var == 50):
print("Value is 50")
elif(var < 50):
print("Value is less than 50")
else:
print("Could not find true expression")
print("Nested IF completed!!")
RQ 5
Multiple Alternative (MultiWay)
if Statements
(a) (b)
6
Flowchart
False
score >= 90
False
True score >= 80
False
grade = 'A' True score >= 70
False
grade = 'B' True score >= 60
grade = 'D'
grade = 'F'
7
Nested If
Which if clause is matched by the else clause? The indentation
indicates that the else clause matches the first if clause in (a) and
the second if clause in (b).
TIP: The code can be simplified by assigning the test value directly
to the variable, as shown in (b)
if number % 2 == 0: even = number % 2 == 0
Equivalent
even = True
else:
even = False This is shorter
(a) (b)
8
Output:
Nested Loops i= 1 j= 1
i= 1 j= 2
Nested loop: loop that is contained inside i= 1 j= 3
another loop i= 2 j= 1
Inner loop goes through all of its i= 2 j= 2
iterations for each iteration of outer loop i= 2 j= 3
i= 3 j= 1
Inner loops complete their iterations i= 3 j= 2
faster than outer loops i= 3 j= 3
Total number of iterations in nested loop: i= 4 j= 1
number_iterations_outer x i= 4 j= 2
i= 4 j= 3
number_iterations_inner
Number of Iterations = 4 x
3 = 12
for i in range(1,5):
for j in range(1,4):
print("i=",i," j=",j)
9
Nested for Loops
10
Nested for Loops
Practice Questions
11
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
13
Nested for Loops
Practice Questions
14
User controlled Nested while
choice = 'y'
RQ 16
break Statement in Python
The break statement terminates the loop
containing it. Control of the program flows to the
statement immediately after the body of the loop.
17
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
The end
18
Python break statement
for letter in 'Python': # First Example
if letter == 'h':
break
print('Current Letter :', letter)
20
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
The end
21
Python continue statement
for letter in 'Python': # First Example
if letter == 'h':
continue
print('Current Letter :', letter)
24
continue
sum = 0
number = 0
25
Post-test Loop with break
// C language post-test (do-while) # Python
// loop
int i = 1;
i=1
do{ while True:
printf("%d\n", i);
i = i + 1; print(i)
} while(i <= 3); i=i+1
Output: if(i > 3):
1
2 break
3
26