java ch 4
Describe the four basic elements of counter-controlled repetition.
1. A controlled variable (or loop counter) 2. The initial value of the control variable. 3. The increment (or decrement) by which the control variable is modified each time through the loop (known as each iteration of the loop). 4. The loop-continuation condition that determines if looping should continue.
Find the error(s) in the following segments of code: The following code should print whether integer value is even or odd: switch (value % 2 ) { case 0: System.out.println("Even integer"); case 1: System.out.println("Odd integer"); }
Each case block should end with a break statement unless it is desired case 0 and case 1 print every time the program is executed. { case 0: System.out.println("Even integer"); break; case 1: System.out.println("Odd integer"); break; }
Find the error(s) in the following segments of code: For( i = 100, i >= 1, i++) System.out.println(i);
In looping statement for 'F' cannot be capital, all the initialization, condition, and increment/decrement must be separated by semicolons. The loop is an infinite loop--i value must be decremented (decreasing) for(i = 100; i >= 1; i--) System.out.println(i);
Find the error(s) in the following segments of code: The following code should output the even integers form 2 to 100: counter = 2; do{ System.out.println(counter); counter += 2; } While (counter > 100);
In while looping statement 'w' must be lowercase. counter = 2; do{ System.out.println(counter); counter += 2; } while (counter > 100);
Compare and contrast the break and continue statements.
The break statement, when executed in a while, for, do...while, or switch, causes immediate exit from that statement. The break statement is used to escape early form a loop or to skip the remainder of switch. The continue statement, when executed in a while, for, or do...while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In a while and do...while statement, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for loop, the increment expression executes then the program evaluates the loop continuation test.
Find the error(s) in the following segments of code: The following code should output the odd integers from 19 to 1: for (i = 19; i >= 1; i += 2) System.out.println(i);
The loop is infinite--i value must be decremented by 2. for (i = 19; i >= 1; i -= 2) System.out.println(i);
Compare and contrast the while and for repetition statements
The while statement can be used to implement any counter controlled loop but the for repetition statement is for specifying the counter controlled repetition detail in a single line of code. The for statements are used for counter controlled repetition and the while statements for sentinel controlled repetition. Both the while and for can each be used for either repetition type.
Discuss a situation in which it would be more appropriate to use a do...while statement than a while. Explain why.
Use do...while instead of while when a set of instructions/block of statements that need to executed at least once. Example: Need to input set of integers in range 1 through 10 and find cumulative sum.