-
What does the following loop output?
(We saw it in
for2.C
.)
How many times does it
iterate
(i.e., repeat itself)?
What is the value of the variable i
during the first iteration?
What is the value of the variable i
during the second iteration?
What is the value of the variable i
during the last iteration?
for (int i {0}; i < 10; ++i) {
cout << i << "\n";
}
-
How is the output different when the loop is changed to
for (int i {0}; i < 10; ++i) {
cout << i << "\n\n";
}
-
How is the output different when the loop is changed to
for (int i {0}; i < 10; ++i) {
cout << i << " ";
}
cout << "\n";
-
Let’s go back to our original loop
for (int i {0}; i < 10; ++i) {
cout << i << "\n";
}
Make it start at 1 instead of at 0.
Then make it start at 5 instead of at 1.
-
What does the following loop output?
Why does it now count up to 10 instead of stopping at 9?
for (int i {0}; i <= 10; ++i) {
cout << i << "\n";
}
-
Make a loop that outputs the integers from 10 to 20 inclusive,
in increasing order,
one after another on the same line, separated by spaces.
See exercise 3 above.
-
What does the following loop output?
What does the +=
do?
for (int i {10}; i <= 100; i += 10) { //i += 10 means i = i + 10
cout << i << "\n";
}
-
Write a loop that outputs
2
4
6
8
-
What does the following loop output?
Why does it count down?
Why does it stop at 0?
for (int i {10}; i >= 0; --i) {
cout << i << "\n";
}
-
What does the following loop output?
Why does it count down by tens?
for (int i {100}; i >= 0; i -= 10) { //i -= 10 means i = i - 10
cout << i << "\n";
}
-
What does the following loop output?
(The
"\t"
is the tab character.)
Why is each number in the second column of output 1 greater than the corresponding
number in the first column of output?
for (int i {0}; i < 100; i += 10) { //i += 10 means i = i + 10
cout << i << "\t" << i+1 << "\n";
}
-
Write a loop whose output is the following.
Each number in the second column of output must be double the corresponding
number in the first column of output.
0 0
10 20
20 40
30 60
40 80
50 100
60 120
70 140
80 160
90 180
Extra credit:
use
setw
to write a loop whose output is the following,
becuase human being like to see their columns of numbers right justified.
For
setw
,
remember to
#include <iomanip>
.
0 0
10 20
20 40
30 60
40 80
50 100
60 120
70 140
80 160
90 180