CS-114 Fundamentals of Programming: Looping Constructs
CS-114 Fundamentals of Programming: Looping Constructs
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)
All programming languages need ways of doing similar things many times!!
Repetition Structures
Introduction to Repetition Structures
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?