You are on page 1of 22

CS-114 Fundamentals Of

Programming
Looping Constructs
Lecture Overview
• Introduction to Repetition Structures
• Types of Loops
• Syntax
• Sentinels
The Need For Repetition (Loops)
• Writing out a simple counting program (1 – 3).
print (1)
print (2)
print (3)

It’s possible to do something repeatedly by just writing it all out!!


The Need For Repetition (2)
• Simple program but what if changes need to be made?
• The source code must be re-edited and executed each time that a change is
needed.

• What if you need the program to count many times?

All programming languages need ways of doing similar things many times!!
Repetition Structures
Introduction to Repetition Structures

• Repetition structure: makes computer repeat included code as


necessary.

Include:
• Condition-controlled loops
• Count-controlled loops
Basic Structure Of Loops
1. Initialize the control
Control –a variable that determines whether or not the loop executes or not.
2. Testing the control against a stopping condition
3. Executing the body of the loop (the part to be repeated)
4. Update the value of the control
Types Of Loops
1. Pre-test loops
• Check the stopping condition before executing the body of the loop.
• The loop executes zero or more times.

2. Post-test loops
• Checking the stopping condition after executing the body of the loop.
• The loop executes one or more times.
Pre-Test Loops
1. Initialize loop control
2. Check if the stopping
condition has been met
a. If it’s been met then the loop
ends
b. If it hasn’t been met then
proceed to the next step
3. Execute the body of the loop
(the part to be repeated)
4. Update the loop control
5. Go to step 2
Post-Test Loops
1. Initialize loop control
2. Execute the body of the loop
(the part to be repeated)
3. Update the loop control
4. Check if the stopping
condition has been met
a. If it’s been met then the loop
ends
b. If it hasn’t been met then return
to step 2.
Loops In Python
Pre-Test Loops In Python
1. While Loop
2. For Loop
3. Nested Loops

Characteristics:
1. The stopping condition is checked before the body executes.
2. These types of loops execute zero or more times.
Post-Test Loops In Python
• None: This type of looping construct has not been implemented
in Python.

Characteristics:
• The stopping condition is checked after the body executes.
• These types of loops execute one or more times.
While Loop In Python
While Loop
• Condition controlled loop
• Syntax:

(Simple)
while (Boolean expression):
body

(Compound)
while (Boolean expression) Boolean operator (Boolean expression):
body
While Loop in Python
• The while loop tells the computer to do something as long as the condition is met.
• It’s construct consists of a block of code and a condition.
• The condition is evaluated, and if the condition is true, the code within the block is executed.
• This repeats until the condition becomes false.

true
Product <= 1000 Product = 2 * Product

false
The While Loop (2)
• Example: Write a program that displays first 10 natural numbers and
displays “Done” when job is finished.

i=1
while (i <= 10):
print ("i =", i)
i += 1
print ("Done!“)
The While Loop (2)
• Example: Write a program that displays first 10 natural numbers and
displays “Done” when job is finished.

Initialize Control

i=1
Check Condition
while (i <= 10):
print ("i =", i) Body
i += 1
Update Control
print ("Done!”)
n = 10
Self-Review Exercise:
• Write a program that counts down: No Yes
n>0?

n=10 print (n)


while n > 0:
print (n) n = n -1
n = n-1
print (“Time Over!”)

print (‘Time Over’)


Self-Review Exercise
• Write a simple program that keeps on asking the user to enter
password until valid password is received. Assume user password is
<user_name@123>
# checks user entered password against a stored password
password = ' '

while password != ‘user_name@123':


print('What is the password?')
password = input()

print('Yes, the password is ' + password + '. You may proceed.')


Infinite Loop
An indefinite loop.

• Loops must contain within themselves a way to terminate


• Something inside a while loop must eventually make the condition false

• Infinite loop: loop that does not have a way of stopping


• Repeats until program is interrupted
• Occurs when programmer forgets to include stopping code in the loop
Infinite Loop Example
#press Ctrl + C to break out of loop
i=1
while True:
i=i+1
print(“Iteration# \t",i)
Questions?

You might also like