Chapter 4 Loops
Chapter 4 Loops
1. count < 100 is always true at Point A. count < 100 always false at Point C. count < 100 is sometimes true or sometimes false at Point B. 2.
It would be wrong if it is initialized to a value between 0 and 100, because it could be the number you attem t to guess!
3.
(a) Infinite number of times. (b) Infinite number of times. (c) The loop body is executed nine times. The printout is 3, 5, 7, 9 on separate lines. 4. max is 5 number 0 5.
" is #$1%&%'()%' *he reason+
,hen a variable is assigned a value that is too large (in size) to be stored, it causes overflow! $1%&%'()%& - 1 is actually #$1%&%'()%' . max is 5 number 0 !. The difference bet"een a do-while loop and a while loop is the order of e#aluatin$ the continuation-condition and executin$ the loop body. In a while loop% the continuation-condition is chec&ed and then% if true% the loop body is executed. In a do-while loop% the loop body is executed for the first time before the continuationcondition is e#aluated.
Scanner input = new Scanner(System.in); int sum = 0; int number; do { System.out.println("Enter an integer " + "(the input ends if it is 0)"); number = input.nextInt(); sum += number;
'. +.
(ame. )hen the i** and **i are used in isolation% their effects are same. The three parts in a for loop control are as follo"s, The first part initiali-es the control #ariable. The second part is a .oolean expression that determines "hether the loop "ill repeat. The third part is the ad/ustment statement% "hich ad/usts the control #ariable. for (int i=1; i<=100; i++) System.out.println(i);
10. sum is 14 count is 5 11. 12. 13. The loop &eeps doin$ somethin$ indefinitely. 0o. The scope of the #ariable is inside the loop. while loop, long sum = 0; int i=0; while (i<=1000) { sum += i++; } do-while loop: long sum = 0; int i = 0; do { sum += i++; } while (i <= 1000); 14. (1) n times (.) n-1 times (2) n#. times
(3) *he ceiling of /n#.01( times 15. 4es. The ad#anta$es of for loops are simplicity and readability. 2ompilers can produce more efficient code for the for loop than for the correspondin$ while loop. 4es. for (int i=1; sum < 10000; i++) sum = sum + i; 1!. 5ine 2, missin$ static. 5ine 3, The semicolon (6) at the end of the for loop headin$ should be remo#ed. 5ine 4, sum not defined. 5ine , the semicolon (6) at the end of the if statement should be remo#ed. 5ine , / not defined. 5ine !, 7issin$ a semicolon for the first println statement. 5ine 11, The semicolon (6) at the end of the "hile headin$ should be remo#ed. 5ine 1', 7issin$ a semicolon at the end of the do8"hile loop. 1'. (1) compile error, i is not initiali-ed. (.) 5ine 3, The 6 at the end of for loop should be remo#ed. for (int i = 0; i < 10; i++); 1+.
When When When When When i i i i is 0, is 1, is 2, is 3,
1 .
the println statement is not executed. the println statement is executed once. the println statement is executed two times. the println statement is executed three times. the println statement is executed nine times.
+ ! + 9 " 45
i is 9,
20. Tip for tracin$ pro$rams, 3ra" a table to see ho" #ariables chan$e in the pro$ram. 2onsider (a) for example. i 1 1 2 / 0 1 0 output 0 0
2 2 3 3 3 3 4 4 4 4 4
1 2 0 1 2 3 0 1 2 3 4 (1).
1 0 1 2 0 1 2 3
0010120123 (.). 9999 9999 2 9999 3 2 9999 4 3 2 9999 (2). 1xxx2xxx4xxx'xxx1 xxx 1xxx2xxx4xxx'xxx 1xxx2xxx4xxx 1xxx2xxx 1xxx (3). 1: 1:3: 1:3:5: 1:3:5:!: 1:3:5:!:+: 21. 22. 0o. Try n1 ; 3 and n2 ;3. The &ey"ord break is used to exit the current loop. The pro$ram in (1) "ill terminate. The output is Balance is 1. The &ey"ord continue causes the rest of the loop body to be s&ipped for the current iteration. The while loop "ill not terminate in (.). 23. If a continue statement is executed inside a for loop% the rest of the iteration is s&ipped% then the action8after8each8iteration is performed and the loop8continuation8
condition is chec&ed. If a continue statement is executed inside a "hile loop% the rest of the iteration is s&ipped% then the loop8continuation8condition is chec&ed. <ere is the fix, int i 2 03 while /i < %0 4 if /i 5 ( 22 00 4 i--3 continue3 6 sum -2 i3 i--3 6 24. class TestBreak { public static void main(string[]args) { int sum = 0; int number = 0; while (number < 20 && sum < 100) { number++; sum += number; } System.out.println("The sum is " + sum); } } class TestContinue { public static void main(String[] args) { int sum = 0; int number = 0; while (number < 20) { number++; if (number != 10 && number != 11) sum += number; } System.out.println("The sum is " + sum); } } 25. (a) After the break statement is executed, the last println statement is executed! *he out ut of this fragment is
1 $ 1 $ $ (