|
Question 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 superfor and for loop, 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 another string.
beginMain /* Here is the superfor loop: */ superfor (var int i=2 to 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
Question 3.1b: Change the superfor loop and the ordinary for looop to print out: "5 6 7 8 9 10".
Question 3.1c: Change the superfor loop and the ordinary for looop to print out: "234 235 236 237 238".
Question 3.1d: Change the superfor loop and the ordinary for looop to print out: the for loop to print out "48 49 50 ... 75 76".
Question 3.1e: Change the for loop to print out "-5 -4 -3 -2 -1 0 1 2 3".
Question 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.
beginMain /* Here is the superfor loop: */ superfor (var int i=10 to 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
Question 3.2b: Change the for loop to print out "20 25 30 35 40".
Question 3.2c: Change the for loop to print out "100 105 110 115 120 125".
Question 3.2d: Change the for loop to print out "2 4 6 8 10 12 14".
Question 3.2e: Change the for loop to print out "10 13 16 19 22 25".
Question 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.
beginMain /* Here is the superfor loop: */ superfor (var int i=10 downto 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
Question 3.3b: Change the for loop to print out "10 9 8 7 6 5 4".
Question 3.3c: Change the for loop to print out "20 19 18 17 16 15 14 13 12".
Question 3.3d: Change the for loop to print out "66 65 64 ... 47".
Question 3.3e: Change the for loop to print out "3 2 1 -1 -2 -3 -4 -5 -6 -7".
Question 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.
beginMain /* Here is the superfor loop: */ superfor (var int i=100 downto 20 step -10) System.out.print(" " + i); System.out.println(); /* Here is the ordinary for loop: */ for (var int i = 100; i>=20; i=i-10) System.out.print(" " + i); System.out.println(); endMain
Question 3.4b: Change the for loop to print out "80 70 60 50 40 30 20".
Question 3.4c: Change the for loop to print out "500 490 480 470 460".
Question 3.4d: Change the for loop to print out "10 8 6 4 2 0".
Question 3.4e: Change the for loop to print out "33 28 23 18 13 8 3".
Question 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 for 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.
beginMain /* Here is the superfor loop: */ superfor (var double i=1.1 to 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); System.out.println(); endMain
Question 3.5b: Change the for loop to print out "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 you would naively expect without running the code.
Question 3.5c: Change the for loop to print out "-30 -19.9 -9.8 0.3 10.4 20.5".
Question 3.5d: Change the for loop to print out "100.0 96.7 93.4 90.1 86.8 83.5 80.2 76.9".
Question 3.5e: Change the for loop to print out "-100.0 -105.5 -111.0 -116.5".
Question 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.
beginMain /* Here is the superfor loop: */ superfor (var char i = 'a' to 'z') 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
Question 3.6b: Change the for loop to print out "a b c d e f".
Question 3.6c: Change the for loop to print out "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 3.6d: Change the for loop to print out "p o n m l k j i h".
Question 3.6e: Change the for loop to print 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".
Back to J.T.W |
This page has the following hit count:
|