Skip to main content\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 5.4 Evaluate Loops-WE2-P1
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.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.
You have attempted
of
activities on this page.