Topic 7: Repetition Structure

The repetition structure, or loop, processes one or more instructions repeatedly. The repeatable instructions in a loop are called the loop body. When a loop’s condition evaluates to true, the instructions in the loop body are processed. Otherwise, the instructions are skipped and processing continues with the first instruction after the loop. After each processing of the loop body (iteration), the loop’s condition is reevaluated to determine if the instructions should be processed again.

It contains four elements:

  • Repetition statement – a body of the loop
  • Condition – a boolean condition that controls whether the instructions are repeated.
  • Initial value – starting value for the condition
  • Loop termination - there must be a statement within the repeating section of code that allows the condition to become false. This is necessary to ensure that, at some point, the repetition stops.

Python has while and for loop. These are pretest loop which is an entrance-controlled loop where the condition is evaluated before the instructions in the loop are processed. If the condition is false, the statements in the loop body will never be executed.

Some loops require the user to enter a special sentinel value to end the loop. Sentinel values should be easily distinguishable from the valid data recognized by the program.

A loop whose instructions are processed indefinitely is called an infinite loop or endless loop .

while loop

while statement is used to create a while loop.

Syntax:
  1. while condition:
  2. statement
  • condition can contain constants, variables, functions, arithmetic operators, comparison operators, and logical operators.
  • statement(s) are processed repeatedly as long as the condition is true.
Example
    1. i = 1
    2. while i <= 10:
    3. print("i: ", i)
    4. i += 1
Output
  • i: 1
    i: 2
    i: 3
    i: 4
    i: 5
    i: 6
    i: 7
    i: 8
    i: 9
    i: 10

for loop

A for loop uses the for statement to code any pretest loop. With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

Syntax:
  1. for item in item_set
  2. statement(s)
  • item is the variable that is going to be used in the statement(s)..
  • item_set item_set represents list, tuple or set.
  • indentaiton is important
Example 1
    1. for x in range(3):
    2. print("Printing:", x)
Output
  • Printing: 0
    Printing: 1
    Printing: 2
Example 2
    1. fruits = ["apple", "banana", "orange"]

    2. for x in fruits:
    3. print(x)
Output
  • apple
    banana
    orange

Counters and Accumulators

A counter can be used to control the loop instead of using a sentinel value. A counter is a numeric variable used for counting something. An accumulator is a numeric variable used for accumulating (adding together) multiple values. It is used to perform calculations such as total or average.

Two tasks are associated with counters and accumulators:

  1. Initializing means assigning a beginning value to a counter or accumulator (usually 0) – happens once, before the loop is processed
  2. Updating (or incrementing) means adding a number to the value of a counter or accumulator. A counter is updated by a constant value (usually 1). An accumulator is updated by a value that varies. Update statements are placed in the body of a loop since they must be performed at each iteration.
Example
    1. fruits = ["apple", "banana", "orange"]
    2. count = 0

    3. for x in fruits:
    4. count +=1

    5. print("Total number of fruits is: ", count)
Output
  • Total number of fruits is: 3

break statement

A break statement forces an immediate break or exit from switch, while, for, and do-while statements. It is useful for breaking out of loops when an unusual condition is detected

Example: Exit the loop when x is "banana”
    1. fruits = ["apple", "banana", "cherry"]

    2. for x in fruits:
    3. if x == "banana":
    4. break
    5. print(x)
Output
  • banana

continue statement

A continue statement applies to while, do-while, and for statements; causes the next iteration of the loop to begin immediately. It is useful for skipping over data that should not be processed in this iteration while staying within the loop

Example: Print number 1 to 9 except for 3
    1. i = 0

    2. while i < 9:
    3. i += 1
    4. if i == 3:
    5. continue
    6. print(i)
Output
  • 1
    2
    4
    5
    6
    7
    8
    9