Iteration

Cards (10)

  • There are two types of iteration in Python – for loops and while loops. 
  • For loops can be used to iterate through a set of steps a specified number of times.
  • The while loop is used when the number of times the loop will be performed is initially unknown. The loop is performed as long as a specified Boolean condition is True. If the condition is False before the while statement is encountered, the loop will be skipped. 
  • Condition-controlled iteration (also known as indefinite iteration) is when a set of instructions is repeated based on whether a condition evaluates as True or False. Types of condition-controlled iteration include while loops, do while loops, and repeat until loops.
  • Count-controlled iteration (also known as definite iteration) is when a set of instructions is repeated a specific number of times. Types of count-controlled loop include for loops and for each loops.
  • A do while loop repeats a set of instructions while a condition is True, but the condition is checked at the end of the loop instead of the start. This is known as a post-condition loop and means that the instructions will be executed at least once, even if the condition initially evaluates to False.
  • Iterative programming involves using repetition statements such as loops to repeatedly execute a block of code until some condition becomes true. It allows us to write programs with fewer lines of code than would otherwise be required by breaking down complex problems into smaller subproblems which are then solved one at a time.
  • Infinite loops occur when there is no exit condition or if an error occurs that prevents the program from exiting the loop. This could happen due to incorrect syntax or logic errors within the code.
  • A repeat until loop keeps executing its body until the condition at the end of the loop evaluates to True. Since the condition of a repeat until loop is checked at the end, these are also a type of post-condition loop. Therefore, the instructions within the loop will be executed at least once, even if the condition evaluates to True at the start.
  • Nested iteration is when a loop is included inside another loop.