Introduction to Repetition in Programming
What is repetition? Something that happens more than
once.
In programming: A section of code that runs more than
once.
Why use repetition?
Reduces code duplication
Makes programs more efficient
Handles large amounts of data
Automates repetitive tasks
Types of loops:
Count-controlled loops (FOR loops)
Condition-controlled loops (WHILE loops)
What is a Count-Controlled Loop?
Definition: A loop that iterates a specific number of times,
determined before the loop begins.
Key Characteristics:
Has a counter variable
Has a starting value
Has an end value
Has an increment value
When to use:
When you know exactly how many times to repeat
When processing items in a sequence
When generating sequences of numbers
Also known as: FOR loops
FOR Loop Syntax in Python
Basic Syntax:
for variable in range(start, end):
statement 1
statement 2
...
Components:
variable : Counter that changes each iteration
range() : Function that defines iteration bounds
statements : Code executed each iteration
Important: Indentation is required for the loop body
Example:
# This loop will output the numbers 0 to 9
for count in range (0, 10):
print (count)
How the Range Function Works
The range() function generates a sequence of
numbers.
Parameters:
start : First number in sequence (default: 0)
stop : Generate numbers up to, but not including this value
step : Increment between numbers (default: 1)
Examples:
range (5)
Output: 0, 1, 2, 3, 4
range (1, 6)
Output: 1, 2, 3, 4, 5
range (0, 10, 2)
Flowcharts for Count-Controlled Loops
Flowcharts visually represent the flow of a program,
including loops.
Key components in a loop flowchart:
Initialization: Set up the counter variable
Condition: Check if the loop should continue
Process: Execute the loop body
Update: Increment/change the counter
Differences from pseudocode:
In pseudocode, FOR loop combines initialization, condition, and
update in one line
In flowcharts, these elements are separated into distinct shapes
Flowcharts show the exact path of execution with arrows
Reading a loop flowchart: Follow the arrows from start to end, noting how the diamond shape creates the loop by directing
flow back to an earlier point when the condition is true.
Key Features of Count-Controlled Loops
Identifying key features in different formats:
Element In Pseudocode In Flowchart
Variable Named in FOR statement Declared before loop
Start Value In range parameters Assigned before loop
End Value In range parameters In condition check
Increment Implied in FOR Explicit statement
Remember:
In pseudocode: for count in range(0, 10)
combines all elements
In flowcharts: Need separate shapes for variable declaration,
condition check, and increment
When analyzing loops: Always identify these four key elements (variable, start value, end value, increment) regardless of the
format.
Practical Examples of FOR Loops
1. Summing Numbers
total = 0
for i in range (1, 11):
total += i
print(total)
Output: 55
2. Processing a List
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for num in numbers:
if num % 2 == 0:
print(num)
Output: 2, 4, 6, 8
Predicting Loop Outcomes
How to predict loop outcomes:
Step-by-Step Approach:
Identify the loop variable and its initial value
Determine the condition for continuing the loop
Trace through each iteration, tracking variable changes
Record the output at each step
Example:
for i in range (1, 5):
print(i * 2)
Iteration i value i*2 Output
1 1 2 2
2 2 4 4
3 3 6 6
4 4 8 8
Summary and Key Takeaways
Key Concepts:
Mind Map of Concepts
Repetition in programming is code that runs more than once
Count-controlled loops (FOR loops) iterate a specific number of What is repetition in an algorithm?
times A piece of code run more than once
FOR loops have a counter, start value, end value, and increment
What is a count-controlled loop?
Code that runs a set number of times
Flowcharts represent loops with initialization, condition, process,
and update
When can a count-controlled loop be useful?
When something has to happen a set number of times
Remember:
In Python, the range() end value is exclusive (not included) What are the key features of a count-controlled loop?
A counter with a starting value, an end value and an increment
Proper indentation is essential in Python loops
Trace through loops step-by-step to predict outcomes