Next: Tutorial 4 Four looping constructs, Previous: Tutorial 2 Introduction to programming in Java, Up: J.T.W. Tutorials [Contents][Index]
Question 4.3.1a: For loops that count up in steps of one. Study the
following code and verify that it prints out "2 3 4 5 6 7 8 9 10" by
compiling and running it. Notice that the System.out.print()
function
call doesn’t print a carriage return after printing the
argument value. That is why the System.out.println()
function
call
is needed at the end of the for loops superfor
and for
, to print a
carriage return at the end of the line. Also note the use of the plus
sign to concatenate a string and the number to produce string result.
class
ForTestbegin
beginMain
) /* Here is the superfor loop: */superfor
(var
int i=2to
10) System.out.print(" " + i); System.out.println(); /* Here is the ordinary for loop: */for
(var
int i=2; i<=10; i=i+1) System.out.print(" " + i); System.out.println();endMain
end
Question 4.3.1b: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "5 6 7 8 9 10".
Question 4.3.1c: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "234 235 236 237 238".
Question 4.3.1d: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "48 49 50 ... 75 76".
Question 4.3.1e: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "-5 -4 -3 -2 -1 0 1 2 3".
Question 4.3.2a: For loops that count up in steps greater than one. Study the following code and verify that it prints out "10 15 20 25 30 35 40" by compiling and running it.
class
ForTestbegin
beginMain
) /* Here is the superfor loop: */superfor
(var
int i=10to
40 step 5) System.out.print(" " + i); System.out.println(); /* Here is the ordinary for loop: */for
(var
int i=10; i<=40; i=i+5) System.out.print(" " + i); System.out.println();endMain
end
Question 4.3.2b: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "20 25 30 35 40".
Question 4.3.2c: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "100 105 110 115 120 125".
Question 4.3.2d: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "2 4 6 8 10 12 14".
Question 4.3.2e: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "10 13 16 19 22 25".
Question 4.3.3a: For loops that count down in steps of one. Study the following code and verify that it prints out "10 9 8 7 6 5 4 3 2 1" by compiling and running it.
class
ForTestbegin
beginMain
/* Here is the superfor loop: */superfor
(var
int
i=10downto
1) System.out.print(" " + i); System.out.println(); /* Here is the ordinary for loop: */for
(var
int i=10; i>=1; i=i-1) System.out.print(" " + i); System.out.println();endMain
end
Question 4.3.3b: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "10 9 8 7 6 5 4".
Question 4.3.3c: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "20 19 18 17 16 15 14 13 12".
Question 4.3.3d: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "66 65 64 ... 47".
Question 4.3.3e: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "3 2 1 -1 -2 -3 -4 -5 -6 -7".
Question 4.3.4a: For loops that count down in steps greater than one. Study the following code and verify that it prints out "100 90 80 70 60 50 40 30 20" by compiling and running it.
class
ForTestbegin
beginMain
/* Here is the superfor loop: */superfor
(var
int i=100downto
20 step -10) System.out.print(" " + i); System.out.println(); /* Here is the ordinary for loop: */for
(var
int=100; i>=20; i=i-10) System.out.print(" " + i); System.out.println();endMain
end
Question 4.3.4b: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "80 70 60 50 40 30 20".
Question 4.3.4c: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "500 490 480 470 460".
Question 4.3.4d: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "10 8 6 4 2 0".
Question 4.3.4e: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "33 28 23 18 13 8 3".
Question 4.3.5a: For loops that use floating point numbers to
count. Study the following code and verify that it prints out "1.1 2.2
3.3 4.4" by compiling and running it. The type name double is short
superfor
double precision floating point. It is natural to ask: why not use
single precision floating point? The answer to this question is that
double precision floating point gives fewer compilation errors than
single precision floating point does.
class
ForTestbegin
beginMain
) /* Here is the superfor loop: */superfor
(var
double i=1.1to
4.4 step 1.1) System.out.print(" " + i); System.out.println(); /* Here is the ordinary for loop: */for
(var
double i=1.1; i<=4.4; i=i+1.1) System.out.print(" " + i);endMain
end
Question 4.3.5b: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "0 2.2 4.4 6.6". Note that rounding errors
may prevent you from getting this exact answer. Also note that the
answer to this question is not what your would naively expect without
running the code.
Question 4.3.5c: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "-30 -19.9 -9.8 0.3
10.4 20.5".
Question 4.3.5d: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "100.0 96.7 93.4 90.1
86.8 83.5 80.2 76.9".
Question 4.3.5e: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "-100.0 -105.5 -111.0
-116.5".
Question 4.3.6a: For loops that use chars to count. Study the following code and verify that it prints out "a b c d e f g h i j k l m n o p q r s t u v w x y z" by and running it.
class
ForTestbegin
beginMain
/* Here is the superfor loop: */superfor
(var
char i='a'to
'z') System.out.print(" " + i); System.out.println(); /* Here is the ordinary for loop: */for
(var
char i='a'; i<='z'; i=i+1) System.out.print(" " + i); System.out.println();endMain
end
Question 4.3.6b: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "a b c d e f".
Question 4.3.6c: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "z y x w v u t s r q p o n m l k j i h g f e d c b a".
Question 4.3.6d: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "p o n m l k j i h".
Question 4.3.6e: Change the superfor
loop and the ordinary for
loop to print out the following numbers: "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".
Next: Tutorial 4 Four looping constructs, Previous: Tutorial 2 Introduction to programming in Java, Up: J.T.W. Tutorials [Contents][Index]