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 6.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 6.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)
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
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
4.
Q9: What is the output of the following loop?
for y in range(100, 10, 1):
print(y)
100 90 80 70 60 50 40 30 20
100 90 80 70 60 50 40 30 20 10
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
You have attempted
of
activities on this page.