Lab Exercise #6 -- Loop the loops
for labs meeting Tuesday Oct. 15, 2002
The purpose of this lab is to go beyond Sect. 3.6-3.8, and explore the behavior
of the for-loop and while-loop constructs of Java. First, let's look at programs53/ForLoop.java. 1. //--------------------------------------------------------------
2. //| Very simple example illustrating for-loop construct
3. //| Author: M. B. Feldman, The George Washington University
4. //| Last Modified: October 2002
5. //--------------------------------------------------------------
6. public class ForLoop
7. {
8. public static void main (String[] args)
9. {
10.
11. for (int counter = 1;
// INITIALIZATION
12. counter
<= 10; // TERMINATION
13. counter++)
// MODIFICATION
14. {
15. System.out.print (counter
+ " ");
16. }
17. }
18. }
Copy and compile this program. What is the output?
Interchange the 1 in line 11 with the 10 in line 12. Compile and run. What
is the output?
Change ++ to -- in line 13. Compile and run; describe the
result.
Change -- back to ++; now add a semicolon after the ) of line 13.
Compile; describe the result.
Remove the spurious semicolon. Now modify the program so it counts backwards
from 10 to 1. Write down the modified lines:
Now modify the program so it displays only the odd numbers. HINT: put the
display statement in a conditional (if) statement inside the loop
body. Write down this conditional statement here:
Now add statements so that the user is prompted for the starting and ending
values. You'll need variables for these -- call them start and end.
Run the program several times; experiment with different starting and ending
values. Try at least one pair such that the starting value
Now let's look at another example, programs53/NestedFor.java.
1. //--------------------------------------------------------------
2. //| Very simple example illustrating nested for-loop construct
3. //| Author: M. B. Feldman, The George Washington University
4. //| Last Modified: October 2002
5. //--------------------------------------------------------------
6. public class NestedFor
7. {
8. public static void main (String[] args)
9. {
10.
11. for (int outer = 1; outer <= 5; outer++)
12. {
13. System.out.print (outer +
" | ");
14. for (int inner = 1; inner
<= 10; inner++)
15. {
16. System.out.print
(inner + " ");
17. }
18. System.out.println("");
19. }
20. }
21. }
Copy, compile, and run it. What is the output?
In line 11, change 5 to 7, and in line 14, change 1 to 6. Now what is the
output?
Now in line 14, change 6 to outer. Now what is the output?
Modify the program so it displays a triangular shape with the point at the
top, like 1 | 1
2 | 1 2
3 | 1 2 3
4 | 1 2 3 4
etc. Write down the modified lines here.
Finally, let's look at a while-loop program, programs53/WhileLoop.java.
This program is logically equivalent to the original ForLoop.java.
1. //--------------------------------------------------------------
2. //| Very simple example illustrating while-loop construct
3. //| Author: M. B. Feldman, The George Washington University
4. //| Last Modified: October 2002
5. //--------------------------------------------------------------
6. public class WhileLoop
7. {
8. public static void main (String[] args)
9. {
10.
11. int counter;
12.
13. counter = 1;
// INITIALIZATION
14. while (counter <= 10)
// TERMINATION
15. {
16. System.out.print (counter
+ " ");
17. counter++;
// MODIFICATION
18. }
19. }
20. }
Copy and compile this one. Write down the output:
Now in line 14, change <= to <. Compile and run; what's the output
now?
Comment out line 17; compile and run it. What's the result?
Finally, remove the comment marks from line 17, but add a semicolon after
the ) in line 14. Now what happens? Why?