Repetition Structure Notes
Repetition Structure Notes
THE DAY
WHY FIT IN WHEN YOU WERE BORN TO STAND OUT?
DR. SEUSS
WORDS OF THE DAY
• COPACETIC
• UPBRAID
ITERATION/REPETION
BACKGROUND INFORMATION
• Algorithms consist of steps that are carried out (performed) one after
another. Sometimes an algorithm needs to repeat certain steps until
told to stop or until a particular condition has been met.
WHY IS ITERATION/REPETITION
IMPORTANT
• Iteration allows us to simplify our algorithm by stating that we will
repeat certain steps until told otherwise.
• If you could kick the person responsible for most of your trouble, you
wouldn’t be able to sit down for days – Theodore Roosevelt
COUNT CONTROLLED LOOPS
• Sometimes it is necessary for steps to iterate a specific number of times.
• Consider this simple algorithm for adding up five inputted numbers:
enter password
unless password = “ilovecomputing”, go back to step 1
say ‘Password correct’
This algorithm would keep iterating until the password is entered correctly. A condition-controlled
loop must be used because there is no way of knowing in advance how many times a password will
need to be entered before it is correct.
A condition-controlled loop is used when it is not known how many times iteration will need to occur.
CONDITION CONTROLLED LOOP
• The pseudocode for this algorithm might look like this:
• password = "blank"
• WHILE password does not equal "ilovecomputing"
• INPUT password
• OUTPUT "Password correct"
• Steps that are part of the loop are indented. Indentation is used to show which
steps are to be iterated.
CONDITION CONTROLLED LOOP
• A count-controlle
COUNT CONTROLLED LOOP
• The Python (3.x) code for this algorithm would look like this:
• total = 0
• for count in range(5):
• number = int(input("Type in a number: "))
• total = total + number
• print("The total is: ")
• print(total)
• Steps that are part of the loop are indented. Indentation tells the computer which steps are to be iterated.
COUNT CONTROLLED LOOP
• The program works like this:
• The program uses the variable ‘count’ to keep track of how many times the iteration has occurred.
• The ‘for’ statement is used to specify where the loop starts.
• The ‘range’ statement specifies how many times the loop is to happen.
• The variable ‘count’ controls the iteration.
• With each iteration, Python automatically adds 1 to the value of ‘count’.
• The program keeps iterating as long as the value of ‘count’ is in the range specified in the loop (in this
case as long as ‘count’ is in the range 0 to 5). Once it reaches the end of the range (5) the iteration
stops.
CONDITION CONTROLLED LOOP
• The Python (3.x) code for this algorithm would look like this:
• total = 0
• answer = "yes"
• while answer == "yes":
• number = int(input("Type in a number: "))
• total = total + number
• answer = input("Any more numbers? yes/no ")
• print("The total is: ")
• print(total)
• Steps that are part of the loop are indented. Indentation tells the computer which steps are to be iterated.
CONDITION CONTROLLED LOOP
• The program works like this:
• total = 0
• answer = input("Any numbers to add? yes/no ")
• while answer == "yes":
• number = int(input("Type in a number: "))
• total = total + number
• answer = input("Any more numbers? yes/no ")
• print("The total is: ")
• print(total)
• If, when the program is run, we entered ‘no’ to the question “Any numbers to add?” then the condition in line 3 would never be
met. The program would skip all the code within the iteration and move onto the last line.
CONDITION CONTROLLED LOOP