Iteration

Cards (50)

  • Iteration
    Repeating steps or instructions over and over again until a particular condition is met
  • Iteration
    • Simplifies an algorithm
    • Is quicker
    • Does not include lots of unnecessary steps
  • Condition
    A situation that is checked every time. If the condition is false the iteration repeats and if it is true the condition stops
  • Counter
    They can keep count of how many iterations have been completed
  • Iteration is the act of repeating a process until there is a desired outcome
  • Real-world examples where humans iterate on a regular basis
    • Practising sports
    • Boiling water
    • Practising an instrument
  • WHILE loop
    1. Index = 0
    2. WHILE index <= 10
    3. OUTPUT("The number is " + index)
    4. Index = index + 1
  • Python WHILE loop
    1. Print("Please insert £8 for a ticket")
    2. While total < 8
    3. coins = int(input("Enter pound coins"))
    4. total = total + coins
    5. print("Amount entered", total)
    6. print("Thank you!")
  • FOR loop
    • Pseudocode uses a variable name and has to show the start and end
    • Python uses a string and you only have to show the end but it doesn't go up to the end number
  • WHILE loop
    • Pseudocode similar to FOR loop but you use < or .
    • Python once you do what is asked the loop is dropped and it proceeds with the code
  • Definite iteration
    The number of iterations that are going to happen before the iteration of the loop
  • WHILE loop algorithm
    1. number = int(input('Please enter your number.'))
    2. Index = 1
    3. While index < = 12:
    4. print(index + 'x' + number + '=' + number + index
    5. INDEX = INDEX + 1
  • Python algorithm for entering low and high numbers
    1. a = int(input("Enter a low number: ")
    2. b = int(input("Enter a higher number: ")
    3. while a <= b
    4. print(a)
    5. a = a+1
  • Indefinite iteration
    The number of iterations is not known before the loop is started
  • Definite iteration
    The number of iterations is known before the execution of the loop is started
  • Python algorithm for summing numbers
    1. sum = 0
    2. n = int(input("Enter numbers: ")
    3. q = input("Do you want to enter more numbers")
    4. While q != "No":
    5. sum = sum + n
    6. q = input("Do you want to enter a number")
    7. N = int(input("Please enter a number"))
    8. print(sum + n)
  • WHILE loop
    The loop continues WHILE a certain condition remains true. The condition is checked before the code is executed
  • REPEAT...UNTIL loop
    The comparison is not done UNTIL the end of the code block
  • Python algorithm for summing numbers with option to enter more

    1. Total = 0
    2. n = int(input("Please enter a number: ")
    3. q = input("Do you want to enter more numbers? y/n")
    4. While q == "y"
    5. total = total + n
    6. N = int(input("Please enter a number: "))
    7. Q = input("Do you want to enter more numbers? ")
    8. Else:
    9. total = total + n
    10. print(total)
  • Python algorithm for guessing game
    1. play = input("Do you want to play? Y?N" )
    2. While play == "Y":
    3. Import random
    4. N = random.randint(1,100)
    5. guess = 0
    6. While guess == 0
    7. guess = int(input("Enter a guess: ")
    8. elif guess > n:
    9. Guess = 0
    10. print("Your guess is too low.")
    11. Else:
    12. print("Well done. You guessed correctly!")
    13. play = input("Do you want to play again?")
    14. print("You have ended the game.")
  • Nesting
    One loop can be run inside another loop
  • Python algorithm for calculating tea and coffee sales
    1. for tea in range(101)
    2. For coffee in range (101)
    3. Total = tea*1.2 + coffee*1.9
    4. If total == 285:
    5. print("Number of tea cups sold", tea)
    6. print("Number of coffee cups sold", coffee)
  • Three building blocks to use when designing algorithms
    • Sequencing
    • Selection
    • Iteration
  • Iteration
    Repeating steps in an algorithm
  • Selection
    A decision
  • Python algorithm for times tables
    1. Num1 = int(input("Enter the first number."))
    2. Num2 = int(intput("Enter the second number"..))
    3. While num1 <= num2
    4. print(f"This is the (num1 times table.")
    5. For x in range(1,13):
    6. print(f"{x} x {num} = (x*num1")
    7. Num1 = num1 + 1
  • Iteration is important as it allows us to simplify an algorithm by removing unnecessary steps
  • Infinite loop

    These are also called endless loops because they go on repeating and never stop
  • Condition
    A situation that is checked every time iteration occurs
  • Infinite loops can be created intentionally to repeat a task indefinitely
  • Placing a condition in a 'WHILE' loop that will always be true can create an infinite loop
  • Testing a condition
    Checking to see if a condition has been met
  • Iteration means that a procedure or a set of statements or commands is repeated a set number of times or until there is a desired outcome
  • Iteration in pseudocode
    REPEAT-UNTIL
  • Iteration in a flowchart
    Represented as a decision
  • WHILE loop
    Runs until a condition becomes false
  • Counter (in iteration)

    Used to keep track of how many times the solution has iterated
  • REPEAT...UNTIL loop
    Runs until a condition becomes true
  • WHILE loop
    Tested at the start of a loop
  • REPEAT...UNTIL loop
    Tested at the end of a loop