Skip to main content
Contents
Prev Up Next Scratch ActiveCode Profile
\(
\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 7.14 Assessment: Nested Loops
Subgoals for Evaluating a Loop.
Determine start condition
Determine update condition
Determine termination condition
Determine body that is repeated
For every iteration of loop, write down values
Exercises Exercises
1.
Q1: What is printed as a result of executing the following code segment?
int count = 0;
for (int x = 0; x < 4; x++) {
for (int y = x; y < 4; y++)
count++;
}
System.out.println(count);
2.
Q2: What is the minimum number of times “Hello” can be printed?
int k = // a random number such that 1 <= k <= n ;
for (int p = 2; p <= k; p++) {
for (int r = 1; r < k; r++)
System.out.println("Hello");
}
3.
Q3: What is printed as a result of executing the following code segment?
for (int r = 3; r > 0; r--) {
int c;
for (c = 1; c < r; c++)
System.out.print("-");
for (c = r; c <= 3; c++)
System.out.print("*");
System.out.println();
}
Figure 7.14.1.
4.
Q4: What is printed as a result of executing the following code segment?
for (int outer = 0; outer < 4; outer++) {
for (int inner = 0; inner <= outer; inner++)
System.out.print(outer + " ");
}
5.
Q5: What is printed as a result of executing the following code segment?
int s = 0;
for (int outer = 1; outer <= 10; outer++) {
for (int inner = outer; inner <= 10; inner++)
s++;
}
System.out.println(s);
You have attempted
of
activities on this page.