final int LIMIT = 10; sum i int sum = 0; ------------ for (int i = 1; i < LIMIT; i = i + 2) { sum = sum + i; }
final int LIMIT = 100; Output ------ for (int i = LIMIT; i > 0; i = i - 10) { System.out.println("**: " + i); }
final int LIMIT = 15; num sum int num = 1; --- --- int sum = 0; do { num = num + 3; sum = sum + num; } while (num < LIMIT); System.out.println ("Last number is " + num + " and sum is " + sum);
String str; int length; System.out.print ("Enter a string of characters: "); str = scan.nextLine(); // Find the length of str ________________________________________________________________; // Write a for loop to print the string backwards
for (int star = 1; star <= 5; star++) System.out.print("*"); System.out.println();The following nested loop prints a triangle of stars (see page 148 of your text - it shows the whole program and the output; page 147 & 148 describe what the program is doing).
final int MAX_ROWS = 10; for (int row = 1; row <= MAX_ROWS; row++) { // Print a row with the same number of stars as variable "row" for (int star = 1; star <= row; star++) System.out.print ("*"); System.out.println(); }
Trace the following similar nested loop (two loops nested inside of one) and show the pattern that would be printed.
final int MAX_ROWS = 4; final int NUM_DASHES = 3; for (int row = 1; row <= MAX_ROWS; row++) { for (int dash = NUM_DASHES; dash > row; dash--) System.out.print ("-"); for (int star = 1; star <= row; star++) System.out.print ("*"); System.out.println(); }