Self-Review Exercises

  1. 5.1 Fill in the blanks in each of the following statements:

    1. Typically,              statements are used for counter-controlled iteration and              statements for sentinel-controlled iteration.

    2. The dowhile statement tests the loop-continuation condition              executing the loop’s body; therefore, the body always executes at least once.

    3. The              statement selects among multiple actions based on the possible values of an integer variable or expression.

    4. The              statement, when executed in an iteration statement, skips the remaining statements in the loop body and proceeds with the next iteration of the loop.

    5. The              operator can be used to ensure that two conditions are both true before choosing a certain path of execution.

    6. If the loop-continuation condition in a for header is initially             , the program does not execute the for statement’s body.

  2. 5.2 State whether each of the following is true or false. If the answer is false, explain why.

    1. The default case is required in the switch selection statement.

    2. The break statement is required in the default case of a switch selection statement to exit the switch properly.

    3. The expression (x > y && a <b) is true if either the expression x > y is true or the expression a < b is true.

    4. An expression containing the || operator is true if either or both of its operands are true.

  3. 5.3 Write a C++ statement or a set of C++ statements to accomplish each of the following:

    1. Sum the odd integers between 1 and 99 using a for statement. Use the unsigned int variables sum and count.

    2. Print the value 333.546372 in a 15-character field with precisions of 1, 2 and 3. Print each number on the same line. Left-justify each number in its field. What three values print?

    3. Calculate the value of 2.5 raised to the power 3 using function pow. Print the result with a precision of 2 in a field width of 10 positions. What prints?

    4. Print the integers from 1 to 20 using a while loop and the unsigned int counter variable x. Print only 5 integers per line. [Hint: When x % 5 is 0, print a newline character; otherwise, print a tab character.]

    5. Repeat Exercise 5.3(d) using a for statement.

  4. 5.4 Find the errors in each of the following code segments and explain how to correct them.

    1.  

      
      unsigned int x{1};
      while (x <= 10); {
         ++x;
      }
      
    2.  

      
      for (double y{0.1}; y != 1.0; y += .1) {
         cout << y << endl;
      }
      
    3. 
      switch (n) {
         case 1:
            cout << "The number is 1" << endl;
         case 2:
            cout << "The number is 2" << endl;
            break;
         default:
            cout << "The number is not 1 or 2" << endl;
      }
      
    4. The following code should print the values 1 to 10.

      
      unsigned int n{1};
      while (n < 10) {
         cout << n++ << endl;
      }
      

Answers to Self-Review Exercises

  1. 5.1

    1. for, while.

    2. after.

    3. switch.

    4. continue.

    5. && (conditional AND).

    6. false.

  2. 5.2

    1. False. The default case is optional. Nevertheless, it’s considered good software engineering to always provide a default case.

    2. False. The break statement is used to exit the switch statement. The break statement is not required when the default case is the last case. Nor will the break statement be required if having control proceed with the next case makes sense.

    3. False. When using the && operator, both of the relational expressions must be true for the entire expression to be true.

    4. True.

  3. 5.3

    1.  

      
      unsigned int sum{0};
      for (unsigned int count{1}; count <= 99; count += 2) {
         sum += count;
      }
      
    2.  

      
      cout << fixed << left
         << setprecision(1) << setw(15) << 333.546372
         << setprecision(2) << setw(15) << 333.546372
         << setprecision(3) << setw(15) << 333.546372 << endl;
      

      Output is:

      
      333.5          333.55         333.546
      
    3. cout << fixed << setprecision(2) << setw(10) << pow(2.5, 3) << endl;

      Output is:

      
      15.63
      
    4.  

      
      unsigned int x{1};
      while (x <= 20) {
         if (x % 5 == 0){
            cout << x << endl;
         }
         else {
            cout << x << '	';
         }
      
         ++x;
      }
      
    5. 
      for (unsigned int x = 1; x <= 20; ++x) {
         if (x % 5 == 0)
            cout << x << endl;
         }
         else {
            cout << x << '	';
         }
      }
      
  4. 5.4

    1. Error: The semicolon after the while header causes an infinite loop.

      Correction: Delete the semicolon after the while header.

    2. Error: Using a floating-point number to control a for iteration statement.

      Correction: Use an unsigned int and perform the proper calculation to get the values.

      
      for (unsigned int y = 1; y != 10; ++y) {
         cout << (static_cast< double >(y) / 10) << endl;
      }
      
    3. Error: Missing break statement in the first case.

      Correction: Add a break statement at the end of the first case. This is not an error if you want the statement of case 2: to execute every time the case 1: statement executes.

    4. Error: Improper relational operator used in the loop-continuation condition.

      Correction: Use <= rather than <, or change 10 to 11.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset