2-Repetition-Structure
Lesson Title
Manuel S. Enverga University Foundation, Lucena City
Autonomous University College of Engineering
Instructor: Hannah Shamira P. Santonil
Learning Outcomes
Introduction to Repetition Structures
The while Loop: A Condition-Controlled Loop
The for Loop: A Count-Controlled Loop
Sentinels
Input Validation Loops
Nested Loops
Introduction to Repetition Structures
Definition: A repetition structure (loop) is used to execute a statement or set of statements repeatedly.
Purpose: It reduces the need for repetitive code, allowing for cleaner and more efficient programming.
Key Concept: Instead of writing the same operations multiple times, code can be encapsulated within a loop that performs the task whenever necessary.
Types of Repetition Structures
Condition-Controlled Loops
Description: Utilizes a true/false condition to dictate repetitions.
Example:
whileloop in Python.
Count-Controlled Loops
Description: Repeats a specified number of times.
Example:
forloop in Python.
The while Loop: A Condition-Controlled Loop
Concept: Executes as long as a condition is true.
Parts of the Loop:
Condition: Tested for true/false.
Execution: Statements repeat while condition is true.
General Syntax of the while Loop
while condition:
statement
statement
...Example of while Loop: Commission Calculation
Program 4-1 (commission.py)
Initialize loop control variable:
keep_going = 'y'Begin while loop:
while keep_going == 'y':Input sales and commission rate from user.
Calculate commission.
Display commission.
Ask user if they want to calculate another commission.
Example Output: Displays commission calculations based on user input.
Notes: The loop terminates when
keep_goingis not 'y'.
The while Loop: Pretest Loop
Definition: Condition is evaluated before the loop executes.
Implication: Certain initial steps may need to be performed to ensure at least one iteration occurs.
Example of while Loop: Temperature Control
Program 4-2 (temperature.py)
Scenario: Monitor the temperature of a substance.
Get initial temperature from user.
If it exceeds a threshold, implement specific actions repeatedly until acceptable temperature.
Display results based on user-defined temperature inputs.
The for Loop: A Count-Controlled Loop
Definition: Iterates a specified number of times using a sequence.
General Syntax:
for variable in [value1, value2, ...]:
statement
...Using the range Function with the for Loop
Purpose: Simplifies creating count-controlled loops.
Example Syntax:
for num in range(5): print(num)
Example of for Loop: Display Numbers
Program 4-4 (simple_loop1.py)
Displays numbers from 1 to 5.
Example of for Loop with a List
Program 4-7 (simple_loop3.py)
Demonstrates looping over a list of strings.
Letting the User Control Loop Iterations
Concept: Allow users to define the maximum loop iterations.
Example: Program modified to accept user input for upper limit, retaining the same mechanics of the loop.
Augmented Assignment Operators
Table 4-1 shows how various assignment operations can be simplified with augmented operators like
+=,-=,*=,/=.
Nested Loops
Definition: A loop within another loop.
Analogy: A clock's hands functioning as nested loops, each performing different iterations.
Example: Clock simulator with nested loops for seconds, minutes, and hours.
Example of Nested Loop: Average Test Scores
Program 4-17 (test_score_averages.py)
Collect number of students and test scores per student.
Loop through students and their respective scores to calculate average.
Output average for each student.