 |
|

|
 |

09/08/09 - Single Loops and Nested Loops
|
 |
 |
 |
  | for (int i = 0; i < 10; ++i) { System.out.print(i + " "); }
|
 |
 |
 |
 |
  | Output: 0 1 2 3 4 5 6 7 8 9
|
 |
 |
 |
 |
  | for (int i = 9; i > 0; --i) { System.out.print(i + " "); }
|
 |
 |
 |
 |
  | Output: 9 8 7 6 5 4 3 2 1
|
 |
 |
 |
 |
  | Incrementing by more than 1
|
 |
 |
 |
 |
  | To increase variable i by 5:
|
 |
 |
 |
 |
  | Decrementing by more than 1
|
 |
 |
 |
 |
  | To decrease variable i by 5:
|
 |
 |
 |
 |
  | Changing x by a factor of 3:
|
 |
 |
 |
 |
  | Replacing x by one-third its value:
|
 |
 |
 |
 |
  | ********** ********** ********** ********** **********
|
 |
 |
 |
 |
  | for (int row = 1; row <= 5; ++row) { // print rows 1 to 5 for (int col = 1; col <= 10; ++col) { // print colums 1 to 10 System.out.print("*"); // print a single * and stay on the same line } System.out.println(); // start a new line }
|
 |
 |
 |
 |
  | for (int row = 1; row <= 5; ++row) { // print rows 1 to 5 for (int col = 1; col <= row; ++col) { // print colums 1 to row System.out.print("*"); // print a single * and stay on the same line } System.out.println(); // start a new line }
|
 |
 |
 |
 |
  | for (int row = 1; row <= 5; ++row) { // print rows 1 to 5 for (int col = 1; col <= 6-row; ++col) { // print colums 1 to row System.out.print("*"); // print a single * and stay on the same line } System.out.println(); // start a new line }
|
 |
 |
 |
 |
  | for (int row = 1; row <= 5; ++row) { // print rows 1 to 5 for (int col = 1; col <= row-1; ++col) { // print 0 to 4 spaces System.out.print(" "); // print a single space } for (int col = 1; col <= 6-row; ++col) { // print colums 1 to row System.out.print("*"); // print a single * and stay on the same line } System.out.println(); // start a new line }
|
 |
 |
 |
 |
  | Assume that you have a variable named count that will take on the values 1, 2, 3, 4,.... You are going to formulate expressions in terms of count that will yield different sequences. For example, to get the sequence 2, 4, 6, 8,... you would use the expression (2*count). Fill in the following table, indicating an expression that will generate each sequence.
Sequence Expression
4, 19, 34, 49, 64, 79,... 30, 20, 10, 0, -10, -20,... -7, -3, 1, 5, 9, 13,... 97, 94, 91, 88, 85, 82,...
|
 |
 |
 |
 |
  | What is the output of the following sequence of loops?
for (int i = 1; i <= 5; ++i) { for (int j = 1; j <= 10; ++j) { System.out.print((i * j) + " "); } System.out.println(); }
|
 |
 |
 |
 |
  | What is the output of the following sequence of loops?
for (int i = 1; i <= 2; ++i) { for (int j = 1; j <= 3; ++j) { for (int k = 1; k <= 4; ++k) { System.out.print(("*"); System.out.print(("!"); } System.out.println(); } }
|
 |
 |
 |
 |
  | Read pp. 78-105 of the text.
|
 |
 |
 |
 |
  | The Fibonacci numbers are a sequence of integers in which the first two elements are 1, and each following element is the sum of the two preceding elements. The first 12 Fibonacci numbers are: 1 1 2 3 5 8 13 21 34 55 89 144. Write a for loop that computes and prints the first 12 Fibonacci numbers.
|
 |
 |
 |
 |
  | It's common to print a rotating, increasing list of single-digit numbers at the start of a program's output as a visual guide to number the columns of the output to follow. With this in mind, write nested for loops to produce the following output, with each line 60 characters wide.
|
 |
 |
 |
 |
  | | | | | | | 123456789012345678901234567890123456789012345678901234567890
|
 |
 |
|


 |
 |
 |