0% found this document useful (0 votes)
22 views

Repetition Structure Notes

The document discusses iteration and repetition in algorithms and programming. It explains that iteration allows algorithms to simplify steps by repeating them until a condition is met. It provides examples of count-controlled loops, which repeat a specific number of times, and condition-controlled loops, which repeat while a condition is true. The examples show how for and while loops implement these patterns in Python code.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Repetition Structure Notes

The document discusses iteration and repetition in algorithms and programming. It explains that iteration allows algorithms to simplify steps by repeating them until a condition is met. It provides examples of count-controlled loops, which repeat a specific number of times, and condition-controlled loops, which repeat while a condition is true. The examples show how for and while loops implement these patterns in Python code.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

ADDITIONAL QUOTE OF

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.

• This makes designing algorithms quicker and simpler because they


don’t have to include lots of unnecessary steps.
ITERATION/REPETITION IN
PROGRAMMING
• Once an algorithm has been designed and perfected, it must be translated – or
programmed – into code that a computer can read.

• We create programs to implement algorithms. Algorithms consist of steps.


Programs consist of statements. A statement is a single instruction - in other
words, a single step.

• Iteration is implemented in programming using FOR and WHILE statements.


FYI

• In programming, iteration is often referred to as ‘looping’, because


when a program iterates it ‘loops’ to an earlier step.
THERE ARE TWO MAIN WAYS THAT
ALGORITHMS LOOP
1. count-controlled loops
2. condition-controlled loops
• Each type of loop works in a slightly different way and produces
different results.
QUOTE OF THE DAY

• 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:

• set the total to 0


• repeat this section five times
• input a number
• add the number to the total
• go back to step 2
• say what the total is
• This algorithm would allow five numbers to be inputted and would work out the total. Because it is known in advance
how many times the algorithm needs to loop, a count-controlled loop is used.
COUNT CONTROLLED LOOP
EXAMPLE
COUNT CONTROLLED LOOPS

• A count-controlled loop is so called because it uses a counter to keep


track of how many times the algorithm has iterated. The pseudocode
for this algorithm might look like this:
COUNT CONTROLLED LOOPS
• total to 0
• count to 1
• FOR as long as count is in the range 1 to 5
• INPUT user inputs a number
• STORE the user's input in the number variable
• total = total + number
• Add 1 to count
• OUTPUT "The total is " + total
• Steps that are part of the loop are indented. Indentation is used to show which steps are to be iterated.
COUNT CONTROLLED LOOPS

• In this example, the variable ‘count’ is used to keep track of how


many times the algorithm has iterated. This variable controls the loop.
The algorithm will continue to iterate until the value of count has
reached 5. As soon as count reaches 5, the algorithm stops iterating.
CONDITION CONTROLLED LOOP

• A condition-controlled loop is so called because iteration continues


while, or until, a condition is met.
CONDITION CONTROLLED LOOP
Consider this simple algorithm for entering a correct password:

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

• In this example, the condition is whether or not the inputted


password equals “ilovecomputing”. The algorithm tests the condition
to see if it is true. If true, the algorithm outputs a message. If false,
the algorithm loops back to the beginning and will continue to do so
until the tested condition is true.
COUNT CONTROLLED LOOP

• Sometimes an algorithm needs to iterate steps a specific number of


times. In programming, count-controlled loops are implemented using
FOR statements. Python uses the statements for and range (note the
lowercase syntax that Python uses):

• for determines the starting point of the iteration


• range states how many times the program will iterate
COUNT CONTROLLED LOOP
• Consider this simple algorithm for adding up five inputted numbers:

• set the total to 0


• repeat this section five times
• input a number
• add the number to the total
• go back to step 2
• say what the total is
• This algorithm would allow five numbers to be inputted and would work out the total. A count-controlled loop would be used
because it is known, in advance, how many times the algorithm needs to 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:

• the ‘while’ statement is used to specify where the iteration starts


• the condition ‘answer = “yes”’ is used to control the iteration. It must be set to “yes” at the beginning so
the code runs at least once
• the program tests the condition by checking if the variable ‘answer’ equals “yes”
• if the condition is true, the program iterates
• if the condition is false, the program moves on to the next step
• This program iterates as many times as is necessary and will keep iterating as long as (while) the
condition is met.
CONDITION CONTROLLED LOOP
• Consider this Python (3.x) program:

• 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

• Infinite Condition Controlled Loops

You might also like