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.2 Evaluate Loops-WE1-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.2.1 Evaluate Loops-WE1-P1
Exercises Exercises
1.
Q1: What is the output of the following loop?
i = 0
while i < 3:
print("hi")
i += 1
See diagram for answer A
See diagram for answer B
See diagram for answer C
See diagram for answer D
See diagram for answer E
2.
Q2: What is the output of the following loop?
i = 10
while i > 1:
print(i, end=" ")
i-=1
10 9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2
10 9 8 7 6 5 4 3 2
compiler error
3.
Q3: What is the output of the following loop?
i = 10
while i > 1:
print(i, end=" ")
i+= 1
10 11 12 13 14 15 16
10 9 8 7 6 5 4 3 2
10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10
infinite loop
4.
Q4: What is the output of the following loop?
i = 0
total = 0
while i <= 50:
total += i
i += 5
print(i)
0 5 10 15 20 25 30 35 40 45 50
0 5 10 15 20 25 30 35 40 45
5 10 15 20 25 30 35 40 45 50
5 10 15 20 25 30 35 40 45
55
5.
Q5: What is the value of counter
after the execution of the folowing code?
counter = 0
while counter > 100:
if counter % 2 == 1:
print(counter + " is odd.")
else:
print(counter + " is even.")
counter += 1
print(counter)
0
1
99
100
101
You have attempted
of
activities on this page.