Skip to main content

Section 5.4 Evaluate Loops-WE2-P1

Subgoals for Evaluating a Loop.

  1. Determine Loop Components
    1. Start condition and values
    2. Iteration variable and/or update condition
    3. Termination/final condition
    4. Body that is repeated based on indentation
  2. Trace the loop, writing updated values for every iteration or until you identify the pattern

Subsection 5.4.1 Evaluate Loops-WE2-P1

Exercises Exercises

1.
    Q6: What is the output of the following loop?
    sum = 0
    for i in range(6):
        sum += i
    print(sum)
    
  • 0
  • 1
  • 15
  • 21
  • infinite loop
2.
    Q7: What is the value stored in total after the following code executes?
    total = 0
    for x in range(50, 0, -5):
        total += x
    
  • 50
  • 0
  • 275
  • 270
  • 5
3.
    Q8: What is the output of the following loop?
    for y in range(0, 10):
        print(y * y, end=" ")
    
  • 0 1 4 9 16 25 36 49 64 81
  • 1 4 9 16 25 36 49 64 81
  • 1 4 9 16 25 36 49 64
  • 0 1 2 3 4 5 6 7 8 9 10
  • 1 2 3 4 5 6 7 8 9 10
4.
    Q9: What is the output of the following loop?
    for y in range(100, 10, 1):
        print(y)
    
  • 100
  • 10
  • 100 90 80 70 60 50 40 30 20
  • 100 90 80 70 60 50 40 30 20 10
  • no output produced
5.
Q10: After the following code is executed:
a = 0
b = 0
for x in range(1, 15):
    if x % 2 == 0:
        a += x
    else:
        b += x
The value of a is and the value of b is
You have attempted of activities on this page.