< previous page page_494 next page >

Page 494
6. What C++ looping statement would you choose for a loop that is both countcontrolled and event-controlled and whose body might not execute even once? (p. 475)
7. When a loop invariant is written as a program comment, where in the loop does it logically, if not physically, belong? (pp. 475479)
Answers
1. switch (nameVal)
   {
        case 1 : cout << Mary;
                 break;
        case 2 : cout << Lynn;
                 break;
        case 3 : cout << Smith;
                 break;   // Not required
   }
2. switch (nameVal)
   {
       case 1 : cout << Mary;
                break;
       case 2 : cout << Lynn;
                break;
       case 3 : cout << Smith;
                break;
       default: cout << Invalid name value.;
                break;   // Not required
   }
3. The body of a Do-While always executes at least once; the body of a While may not execute at all.
4. for (count = 10; count >= 1; count--) 5. A continue statement terminates the current iteration and goes on to the next iteration (if possible). A break statement causes an immediate loop exit. 6. Either a While or a For statement 7. Immediately before the loop test.
Exam Preparation Exercises
1. Define the following terms:
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
switch expression
pretest loop
posttest loop
2. A switch expression may be an expression that results in a value of type int, float, or char. (True or False?)
3. The values in case labels may appear in any order, but duplicate case labels are not allowed within a given Switch statement. (True or False?)
4. All possible values for the switch expression must be included among the case labels for a given Switch statement. (True or False?)
5. Rewrite the following code fragment using a Switch statement.

 
< previous page page_494 next page >