Section 5.1 While Counter
Subgoals for Evaluating a Loop.
-
Determine Loop Components
-
Start condition and values
-
Iteration variable and/or update condition
-
Termination/final condition
-
Body that is repeated based on indentation
-
-
Trace the loop, writing updated values for every iteration or until you identify the pattern
Subsection 5.1.1
Problem: Given the following code, what is the output?
counter = 0
total = 0
while counter < 50:
if counter % 5 == 0:
total += counter
counter += 1
print(total)
Subsection 5.1.2 Determine loop components
Starting values:
counter = 0
total = 0
Termination condition:
while counter < 50:
Body that is repeated based on indentation:
if counter % 5 == 0:
total += counter
counter += 1
Subsection 5.1.3 Trace the loop, writing updated values for every iteration or until you identify the pattern
For every iteration of loop, write down values.

counter increments by 1 But only when the counter is evenly divisible by 5 is the value added to total.


Answer.
Output is
225
You have attempted of activities on this page.
