0% found this document useful (0 votes)
20 views26 pages

ProgFund Lect Week 6

The document discusses nested control structures in Python including nested if, else statements and for, while loops. It provides examples of using break and continue statements in nested loops to terminate or skip iterations.

Uploaded by

Battle Gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views26 pages

ProgFund Lect Week 6

The document discusses nested control structures in Python including nested if, else statements and for, while loops. It provides examples of using break and continue statements in nested loops to terminate or skip iterations.

Uploaded by

Battle Gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Programming Fundamentals

(SWE – 102)

Nested Control Structures


Multiple if Statements
 Sometimes you want to check for all condition
independently not relating to each other and condition
which is True will be executed. In this situation we use
Multiple if Statements
x=1
if x==1:
print("One")
if x<10:
print("x is less than 10")
if x==10:
print("x equals 10")
if x>10:
print("x greater than 10")
2
Nested if
 There may be a situation when you want to check for
another condition after a condition resolves to true. In
such a situation, you can use the nested if construct.

 One if statement can be placed inside another if statement


to form a nested if statement.
if i > k:
if j > k:
print("i and j are greater than k")
else:
print("i is less than or equal to k")

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

if score >= 90.0: if score >= 90.0:


grade = 'A' grade = 'A'
else: elif score >= 80.0:
if score >= 80.0: Equivalent grade = 'B'
grade = 'B' elif score >= 70.0:
else: grade = 'C'
if score >= 70.0: elif score >= 60.0:
grade = 'C' grade = 'D'
else: else:
if score >= 60.0: This is better grade = 'F'
grade = 'D'
else:
grade = 'F'

(a) (b)

6
Flowchart

False
score >= 90
False
True score >= 80
False
grade = 'A' True score >= 70
False
grade = 'B' True score >= 60

grade = 'C' True

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

 Nestedloop: 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).

10
Nested for Loops

Practice Questions

 Problem: Write a program that uses nested for


loops to print a Factorial result from (2 – 10).

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

 Problem: Write a program that uses nested


while loops to print a multiple tables from
(2 – 10).

13
Nested for Loops

Practice Questions

 Problem: Write a program that uses nested


while loops to print a Factorial result from
(2 – 10).

14
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 15
User controlled Nested while

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.

 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 inner and outer 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)

var = 10 # Second Example


while var > 0:
var = var -1
if var == 5:
break
print('Current variable value :', var)
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.

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)

var = 10 # Second Example


while var > 0:
var = var -1
if var == 5:
continue
print('Current variable value :', var)
print("Program Complete!")
RQ 22
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 : 0
Program Complete!
RQ 23
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)

24
continue
sum = 0
number = 0

while (number < 20):


number += 1
Jump to the if (number == 10 or number == 11):
end of the continue
iteration sum += number

print("The sum is ", sum)

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

You might also like