4.1: Introduction to Repetition Structures
● A repetition structure causes a statement or set of statements to execute repeatedly
● A condition-controlled loop uses a true/false condition to control the number of times that it
repeats
● A count-controlled loop repeats a specific number of times
4.2: The while Loop: A Condition-Controlled Loop
● While a condition is true, do some task
● Two parts: a condition is tested to be true or false, and a statement is executed if it is true
● while condition:
statement
statement
● If the statement is false, it exits the loop
● Each execution of the body of a loop is known as an iteration
The while Loop is a Pretest Loop
● The while loop is a pretest loop, meaning it tests the condition before performing an iteration
● Usually, you have to write some steps before the loop for it to iterate
Infinite Loops
● Almost always, loops must contain a way to terminate itself; something must make it false
eventually
● An infinite loop continues until the program is interrupted
● You should not write infinite loops; they are usually
mistakes
4.3: The for Loop: A Count-Controlled Loop
● Count-controlled loops iterate a specific number of times
● for variable in [value1, value2, etc…]
statement
statement
, ● The variable used in the for clause is the target variable
Using the range Function with the for Loop
● The range function creates an iterable
○ An iterable is an object similar to a list; it contains a sequence of values that can be
iterated over with something like a loop
● Instead of using a list of values, you can pass a number as an
argument
○ for num in range(5):
print(num)
○ This prints 5 numbers
○ Whatever number you put in the range, it goes from 0
to, but not including, that number
○ That code above is the same as:
for num in range[0, 1, 2, 3, 4]
print(num)
● If you pass 1 number as an argument, it acts as the end number with the starting number being 0;
if you pass 2 numbers, the first number is the starting number and the second is the end
● If you pass 1 or 2 numbers, each number increases by 1 in the range; if you pass 3, the 3rd
number acts as the interval (i.e. if you put 2, each number increases by 2); this is called the step
value
Using the Target Variable Inside the Loop
● Target variables reference each item in a sequence as the loop iterates
● Each time the loop iterates, the variable references the next number in the range
Letting the User Control Loop Iterations
● Sometimes, the user needs to decide how many loops need to be created
● A repetition structure causes a statement or set of statements to execute repeatedly
● A condition-controlled loop uses a true/false condition to control the number of times that it
repeats
● A count-controlled loop repeats a specific number of times
4.2: The while Loop: A Condition-Controlled Loop
● While a condition is true, do some task
● Two parts: a condition is tested to be true or false, and a statement is executed if it is true
● while condition:
statement
statement
● If the statement is false, it exits the loop
● Each execution of the body of a loop is known as an iteration
The while Loop is a Pretest Loop
● The while loop is a pretest loop, meaning it tests the condition before performing an iteration
● Usually, you have to write some steps before the loop for it to iterate
Infinite Loops
● Almost always, loops must contain a way to terminate itself; something must make it false
eventually
● An infinite loop continues until the program is interrupted
● You should not write infinite loops; they are usually
mistakes
4.3: The for Loop: A Count-Controlled Loop
● Count-controlled loops iterate a specific number of times
● for variable in [value1, value2, etc…]
statement
statement
, ● The variable used in the for clause is the target variable
Using the range Function with the for Loop
● The range function creates an iterable
○ An iterable is an object similar to a list; it contains a sequence of values that can be
iterated over with something like a loop
● Instead of using a list of values, you can pass a number as an
argument
○ for num in range(5):
print(num)
○ This prints 5 numbers
○ Whatever number you put in the range, it goes from 0
to, but not including, that number
○ That code above is the same as:
for num in range[0, 1, 2, 3, 4]
print(num)
● If you pass 1 number as an argument, it acts as the end number with the starting number being 0;
if you pass 2 numbers, the first number is the starting number and the second is the end
● If you pass 1 or 2 numbers, each number increases by 1 in the range; if you pass 3, the 3rd
number acts as the interval (i.e. if you put 2, each number increases by 2); this is called the step
value
Using the Target Variable Inside the Loop
● Target variables reference each item in a sequence as the loop iterates
● Each time the loop iterates, the variable references the next number in the range
Letting the User Control Loop Iterations
● Sometimes, the user needs to decide how many loops need to be created