Nothing Special   »   [go: up one dir, main page]

0% found this document useful (0 votes)
21 views24 pages

Python Chapter 5.3-5.4

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 24

5.

3 break and continue statement


5.4 nested loop
▪ Review on iteration
▪ Explain how continue works
▪ Explain how break works
▪ Explain nested loop concept and how it works
INITIALIZE
LCV

▪ Contains 3 important parts:

▪ Initialize TEST
LCV
▪ Loop test

▪ Updating STATEMENTS
TO REPEAT
STATEMENTS

UPDATE
LCV
for i in range(1,10):
print(i)

i=1

i<10

print(i)

i=i+1
Initialize LCV
while condition_test_LCV:
statements_to_repeat
Update LCV
Statements
INITIALIZE
LCV

▪ A loop is controlled by Loop


Controlled Variable (LCV) TEST false
▪ The LCV is initialized before tested LCV

▪ LCV is tested to ensure the loop limit true


is accessed.
STATEMENTS
▪ If the LCV test result is true, TO REPEAT
STATEMENTS
▪ Execute statements in the loop body
▪ Update LCV UPDATE
▪ Repeat the process LCV

▪ Otherwise, loop is terminated and


execute statements follows the loop
▪ A loop should be constructed so that it can terminate when a specific condition is
accessed.
▪ A loop that cannot stop is called Infinite Loop
▪ An infinite loop executes forever unless an even occur in a loop and the loop
breaks.
▪ A perfect loop also can break although it has not completing the iteration.
▪ A break; statement is used to break a loop before its time or to break an infinite
loop
▪ Before executing break statement, a condition (event) is set using if statement
loop
condition
False

True
statements

event
condition
True

False
break
statements
The following loop executes 50 times. For each Questions
execution, it displays the value of its loop control
variable (lcv).
▪ How many times the loop executes? Explain
lcv=1
your answer!
while lcv <=50:
print("value of lcv = ",lcv)
lcv=lcv + 1 ▪ How many times does the following statement
be executed?
print("loop stop at lcv value ",lcv)
print("loop stop at lcv value ",lcv)
The loop is then modified by adding an
QUESTIONS
event
▪ How many times the loop should
lcv=1 executes?
while lcv <=50:
If lcv==47:
break ▪ How many time the loop executes?
print("value of lcv = ",lcv)
lcv=lcv + 1
▪ How many times statement lcv=lcv+1
print("loop stop at lcv value ",lcv) executes?

▪ What is the last value of lcv?

▪ What happen when lcv==47?


Observe how the following loop executes. Questions

▪ How many times the loop executes? Explain


lcv=True
your answer.
while lcv:
print("value of lcv = ",lcv)
▪ Does the statement
print("End of Loop")
print("end of Loop")
be executed?

Can the loop be modified so that it can


terminate?
The following loop executes infinitely Questions
until an event breaks it.
▪ How many times the loop executes? Explain
import random
your answer!
lcv=True
key=random.randint(1,10)
▪ Can the following statement be accessed?
while lcv: Explain your answer.
x=int(input("Enter a value print("loop terminates")
between 1-10"))
if x==key:
break
print("loop terminates")
▪ Similar to break statement, the continue statement is applied in a loop.
▪ Both works for while loop or for loop
▪ Break statement terminates the loop before its time and skip the remaining
statements while continue statement returns the control to the beginning of the
loop and rejectc all the remaining statements in the current iteration of the loop
and moves the control back to the top of the loop.
loop
False
condition

True
statements

event1 True
condition

False break

True
event2
condition

continue
False
statements
▪ Observe the output produced by the following ▪ QUESTIONS
codes ▪ What happen when letter ‘h’ is
encountered in the loop?
for letter in 'Python':
if letter == 'h':
continue
print ('Current Letter :', letter)
▪ Observe the output produced by the QUESTIONS
following codes: ▪ What values omitted by the loop?
Explain your answer.
var = 10
while var > 0:
var = var -1
if var%3 == 0:
continue
print ('Current variable value :', var)
print ("Good bye!")
▪ Observe the output produced by the following codes.
import random
lcv=1
key=random.randint(1,11)
while lcv<=20:
x=int(input("Enter a value between 1-10: "))
if x==key:
print("you found it.")
break
if x==key-1 or x==key+1:
print("add or subtract 1")
continue
lcv=lcv+1
print("Total trial left ",20-lcv)
print("loop terminates")
▪ Explain the term nested loop
▪ Demonstrate how nested loop works
▪ A loop that executes inside other loop
▪ Construct multiplication table

import time
for i in range (1,3):
print("Multiplication table ",i)
for j in range (1,12):
z=i*j
print("{} x {} = {}".format(i,j,z))
time.sleep(2)
Multiplication table 1
1x1=1
1x2=2
1x3=3
for i in range (1,3): 1x4=4 for j in range (1,12):
1x5=5
i=1 1x6=6 J=1..12
1x7=7 (inner loop)
1x8=8
1x9=9
1 x 10 = 10
1 x 11 = 11
1 x 12 = 12
i=1..2
(outer loop) Multiplication table 2
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
i=2 2 x 6 = 12 J=1..12
2 x 7 = 14 (inner loop)
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
2 x 11 = 22
2 x 12 = 24
▪ Complete the following loop so that it can solve the following Sampel Output:
problem: Enter an ID: 1111
Enter sales at Quarter 1: 5000
Read data for 5 salesperson. For each salesperson, request
salesperson ID(4 digits), total sales for 4 quarters in a year. Enter sales at Quarter 2: 1200
Calculate the sum of the total sales for the whole year. Enter sales at Quarter 3:10000
Enter sales at Quarter 4: 20000
for I in range ( ?): Total sales a year for 1111 is 36200
ID=?
sum=0 Enter an ID: 1121
Enter sales at Quarter 1: 500
for j in range(?): Enter sales at Quarter 2: 2000
str1=“Enter sales at Quarter “+str(j) Enter sales at Quarter 3:11000
Enter sales at Quarter 4: 20000
totalsales=int(input(str1)) Total sales a year for 1121 is 33500
sum=?
print(“Total sales a year for {} is {}”.format(ID, sum)) Enter an ID: 1121
:
:

You might also like