What do the following loops print? Work out the answer by tracing the code, not by using the computer. a. int s = 1; for (int n = 1; n <= 5; n++) { s = s + n; System.out.print(s + " "); }
b. int s = 1; for (int n = 1; s <= 10; System.out.print(s + " ")) { n = n + 2; s = s + n; } c. int s = 1; int n; for (n = 1; n <= 5; n++) { s = s + n; n++; } System.out.print(s + " " + n);
6.19 What do the following program segments print? Find the answers by tracing the code, not by using the computer.
a. int n = 1; for (int i = 2; i < 5; i++) { n = n + i; } System.out.print(n);
b. int i; double n = 1 / 2; for (i = 2; i <= 5; i++) { n = n + 1.0 / i; } System.out.print(i);
c. double x = 1; double y = 1; int i = 0; do { y = y / 2; x = x + y; i++; } while (x < 1.8); System.out.print(i);
d. double x = 1; double y = 1; int i = 0; while (y >= 1.5) { x = x / 2; y = x + y; i++; } System.out.print(i);