Self-Review Exercises

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

    1. Typically,                     statements are used for counter-controlled iteration and statements are used 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 for statement’s body does not execute.

    7. Methods that perform common tasks and cannot be called on objects are called                     methods.

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

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

    2. The break statement is required in every case of a switch statement.

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

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

    5. The integer after the comma (,) in a format item (e.g., {0,4}) indicates the field width of the displayed string.

    6. To test for a range of values in a switch statement, use a hyphen () between the start and end values of the range in a case label.

    7. Listing cases consecutively with no statements between them enables the cases to perform the same set of statements.

  3. 6.3 Write a C# statement or a set of C# statements to accomplish each of the following tasks:

    1. Sum the odd integers between 1 and 99, using a for statement. Assume that the integer variables sum and count have been declared.

    2. Calculate the value of 2.5 raised to the power of 3, using the Pow method.

    3. Display the integers from 1 to 20 using a while loop and the counter variable i. Assume that the variable i has been declared, but not initialized. Display only five integers per line. [Hint: Use the calculation i % 5. When the value of this expression is 0, display a newline character; otherwise, display a tab character. Use the Console.WriteLine() method to output the newline character, and use the Console.Write(' ') method to output the tab character.]

    4. Repeat part (c), using a for statement.

  4. 6.4 Find the error(s) in each of the following code segments and explain how to correct it:

    1. 
      i = 1;
      while (i <= 10);
         ++i;
      }
      
    2. 
      for (k = 0.1; k != 1.0; k += 0.1)
      {
         Console.WriteLine(k);
      }
      
    3. 
      switch (n)
      {
         case 1:
            Console.WriteLine("The number is 1");
         case 2:
            Console.WriteLine("The number is 2");
            break;
         default:
            Console.WriteLine("The number is not 1 or 2");
            break;
      }
      
    4. The following code should display the values 1 to 10:

      
      n = 1;
      while (n < 10)
      {
         Console.WriteLine(n++);
      }
      
..................Content has been hidden....................

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