Iteration#

From Busbee and Braunschweig’s “Iteration Control Structures,” in Programming Fundamentals:
“In iteration control structures, a statement or block is executed until the program reaches a certain state, or operations have been applied to every element of a collection. This is usually expressed with keywords such as while, repeat, for, or do..until. The basic attribute of an iteration control structure is to be able to repeat some lines of code. The visual display of iteration creates a circular loop pattern when flowcharted, thus the word ‘loop’ is associated with iteration control structures.”
Let’s break down that definition:
Iteration (repetition of a process) is a type of control structure
In programming languages, iteration involves lines of code repeating until a condition is met of the end of a group of values has been reached.
Because iteration in this context involves a circular pattern, many object-oriented programming languages refer to these structures as
loops
Additional Resources
For more on the technical foundations of iteration in Python:
W3Schools, “Python Iterators”
Iteration in Python#
We’ve actually already seen iteration in action.
# create string variable
message = "Hello world!"
# return number of characters in string
len(message)
# test if character x is in string
'x' in message # returns false
# test if symbol % is NOT in string
'%' not in message # returns true
True
These membership tests (using in and not in) involve iteration- the program goes left-to-right, starting at the first character in the string. You may remember we used .index() in the data storage lab to search for characters that appear multiple times in a string- Python only returned the first occurrence of the character because of how iteration impacted the .index() method.
Another example of iteration in action, this time using a list:
# list of string objects
fruits = ["apple", "banana", "blueberry", "cherry", "pear"]
# return index for cherry
print("The index for cherry is ", fruits.index("cherry"))
# return index for pear
print("The index for pear is ", fruits.index("pear"))
# test if apple is in list
print("apple" in fruits)
# test if blueberry is NOT in list
print("blueberry" not in fruits)
The index for cherry is 3
The index for pear is 4
True
False
Iteration works similarly in a list- the program goes left-to-right, starting with the first element in the list.
Loops#
The power of iteration as a control structure comes from a programming concept called loops.

Most high-level programming languages support two main types of loops: event-controlled and count-controlled
Event-controlled loops test for an initial condition, and execution continues as long as the initial condition is
True. How many times the loop will execute is not known.Count-controlled loops (sometimes called counter-controlled loops) continue executing for a pre-determined number of times. The number of times the loop will execute is known because the loop is iterating through a collection of objects/values (i.e. a string or list), or the iteration when the initial condition will no longer be
Trueis known.
Let’s look at additional details and Python examples for each.