Self-Review Exercises

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

    1. All apps can be written in terms of three types of control structures:                , and                .

    2. The                 statement is used to execute one action when a condition is true and another when that condition is false.

    3. Repeating a set of instructions a specific number of times is called                 iteration.

    4. When it’s not known in advance how many times a set of statements will be repeated, a(n)                 value can be used to terminate the iteration.

    5. The                 structure is built into C#—by default, statements execute in the order in which they appear.

    6. Instance variables of type int are given the value                 by default.

    7. C# requires all variables to have a(n)                .

    8. If the increment operator is                 to a variable, the variable is incremented by 1 and its new value is used in the expression.

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

    1. An algorithm is a procedure for solving a problem in terms of the actions to execute and the order in which these actions execute.

    2. A set of statements contained within a pair of parentheses is called a block.

    3. A selection statement specifies that an action is to be repeated while some condition remains true.

    4. A nested control statement appears in the body of another control statement.

    5. C# provides the arithmetic compound assignment operators +=, -=, *=, /= and %= for abbreviating assignment expressions.

    6. Specifying the order in which statements (actions) execute in an app is called program control.

    7. The unary cast operator (double) creates a temporary integer copy of its operand.

    8. Instance variables of type bool are given the value true by default.

    9. Pseudocode helps you think out an app before attempting to write it in a programming language.

  3. 5.3 Write four different C# statements that each add 1 to int variable x.

  4. 5.4 Write C# statements to accomplish each of the following tasks:

    1. Assign the sum of x and y to z, and increment x by 1 with ++. Use only one statement and ensure that the original value of x is used in the statement.

    2. Test whether variable count is greater than 10. If it is, display "Count is greater than 10".

    3. Decrement the variable x by 1, then subtract it from the variable total. Use only one statement.

    4. Calculate the remainder after q is divided by divisor, and assign the result to q. Write this statement in two different ways.

  5. 5.5 Write a C# statement to accomplish each of the following tasks:

    1. Declare the int variable sum and initialize it to 0.

    2. Declare the int variable x and initialize it to 1.

    3. Add variable x to variable sum, and assign the result to variable sum.

    4. Display "The sum is: ", followed by the value of variable sum.

  6. 5.6 Combine the statements that you wrote in Exercise 5.5 into a C# app that calculates and displays the sum of the integers from 1 to 10. Use a while statement to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11.

  7. 5.7 Determine the values of the variables in the following statement after it executes. Assume that when the statement begins executing, all variables are type int and have the value 5.

    product *= x++;
    
  8. 5.8 Identify and correct the errors in each of the following sets of code:

    1. 
       while (c <= 5)
      {
          product *= c;
          ++c;
      
    2. 
      if (gender == 1)
      {
         Console.WriteLine("Woman");
      }
      else;
      {
         Console.WriteLine("Man");
      }
      
  9. 5.9 What is wrong with the following while statement?

    
    while (z >= 0)
    {
       sum += z;
    }
    
..................Content has been hidden....................

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