You are on page 1of 30

Session # 4.

a
Chapter 4

Tamer Elsayed
CSE Dept.
Start

i = 1

no
i <= 5?

yes

print i

End
How can we
i = i + 1
implement it?

CMPS 151 Programming Concepts 2


grade1 = float(input('Enter first grade'))
grade2 = float(input('Enter second grade'))
grade3 = float(input('Enter third grade'))
avg = (grade1 + grade2 + grade3)/3
print('Average = ', avg)

 Calculates the average of 3 grades for one student.

 What if 10 students? 100 students?


 We need to repeat the above code 10 or 100 times!

CMPS 151 Programming Concepts 3


 Makes program long
 Time consuming
 Makes programming boring!
 May need to be corrected in many places

Instead, we use
repetition structures (loops)

CMPS 151 Programming Concepts 4


A loop is a programming structure that
allows a block of statements to repeat until it
meets (or no longer meets) a given condition.

CMPS 151 Programming Concepts 5


Start

i = 1

no
i <= 5?

yes

print i

End
i = i + 1

CMPS 151 Programming Concepts 6


 Reached last student in list.
 Checked out from a menu (no more extra orders).
 The correct password is entered.

CMPS 151 Programming Concepts 7


Each repetition in a loop is called an iteration.
Start

i = 1

no
i <= 5?

yes

print i
End repeated block
i = i + 1

CMPS 151 Programming Concepts 8


Types of repetition (loops)

condition-controlled loops count-controlled loops


while Loop for Loop

CMPS 151 Programming Concepts 9


CMPS 151 Programming Concepts 10
false
condition

true

Statement(s)

CMPS 151 Programming Concepts 11


false
condition

while condition: true

statement(s) Statement(s)

 condition is evaluated before every iteration


● if it is true, the statement(s) are executed, and then
condition is evaluated again.
● if it is false, the loop is terminated and the program continues
right after the loop.
 If the condition is initially false, the statement(s) in the body of
the loop are never executed.
CMPS 151 Programming Concepts 12
 What is the output of this program? Start

i = 1
i = 1
while i <= 5:
print(i)
i = i + 1 no
i <= 5?

Output: 1 yes
2
3 print i
4
5 End i = i + 1

For a loop to stop, something has to happen in the


body to make the condition false.
CMPS 151 Programming Concepts 13
i = 1
while i <= 5:
print(i)
i = i + 1

 What if we want to print 5 down to 1?

 What if we want to print even positive numbers < 100?

CMPS 151 Programming Concepts 14


Our
Intention!

CMPS 151 Programming Concepts 15


Programming is FUN!

CMPS 151 Programming Concepts 16


i = 1
while i <= 5:
print(i)
i = i + 1

 What if we want to print 5 down to 1?

 What if we want to print even positive numbers < 100?

CMPS 151 Programming Concepts 17


 while is a pretest loop (condition is evaluated before
the loop executes).

 If the condition is initially false, the statement(s) in the


body of the loop are never executed.

 If the condition is initially true, the statement(s) in the


body continue to be executed until the condition
becomes false.

 Requires performing some steps prior to the loop. In the


previous example, we assign 1 to i before the loop.

CMPS 151 Programming Concepts 18


 What is the output of the following?

i = 5
while i > 0:
print(i)

Infinite loop!

CMPS 151 Programming Concepts 19


 The loop must contain code to allow condition to
eventually become false so the loop stops.
 Otherwise, you have an infinite loop (i.e., a loop that
does not stop)
 Example infinite loop:
i = 5
while i > 0: # infinite loop because
print(i) # x is always > 0
# and nothing is changing it!

How can we fix it?


CMPS 151 Programming Concepts 20
i = 5
while i > 0:
print(i)
i = i - 1

Don’t forget indentation!

CMPS 151 Programming Concepts 21


CMPS 151 Programming Concepts 22
 Loops are an appropriate structure for validating user
input data

read data
while (data is invalid)
print error
read data
# data is valid here

CMPS 151 Programming Concepts 23


1. Prompt for and read in the data.
2. Use a while loop to test if data is valid.
3. Enter the loop only if data is not valid.
4. Inside the loop, display error message and prompt the
user to re-enter the data.
5. The loop will not be exited until the user enters valid
data.

CMPS 151 Programming Concepts 24


score = int(input("Enter a test score: "))
while (score < 0 or score > 100)
print("Invalid number!")
print("Enter valid score between 1 and 100.")
Score = int(input("Enter a test score: "))
# Code to use the valid number goes here.

CMPS 151 Programming Concepts 25


 What if we do not know how many values the user
wants to enter ahead of time?

 For example, expenses in a trip.

CMPS 151 Programming Concepts 26


CMPS 151 Programming Concepts 27
 A sentinel is value in a list of values that indicates end of
data.
 Special value that cannot be confused with a valid value,
e.g., -999 for a test score.
 Used to terminate input when user may not know how
many values will be entered.

CMPS 151 Programming Concepts 28


0 is the
sentinel

CMPS 151 Programming Concepts 29


Write a program that keeps asking the user to enter
a number and checks if it is odd or even until the
user enters 0.

CMPS 151 Programming Concepts 30

You might also like