Next: Exercises Up: The if-else Statement Previous: Multiple Choice Questions

Review Questions

  1. If x has the value 3.5 when the following statement is executed what value would be assigned to y?
    if (x + 1 <= 3.6)
       y = 1.0;
    else
       y = 2.0;
    
  2. In words what do you think the effect of the following statement is intended to be? Why is the statement syntactically incorrect? How could it be changed to be syntactically correct? Has the change that you have made ensured that the statement actually carries out the logical effect you stated at the beginning?
    if (x >= y)
       sum += x;
       cout << "x is bigger" << endl;
    else
       sum += y;
       cout << "y is bigger" << endl;
    
  3. Write an if-else statement which would add a variable x to a variable possum if x is positive and would add x to negsum if the variable x was negative.
  4. Expand the solution to the previous question so that if x is positive then a variable poscount is incremented and if x is negative a variable negcount is incremented. If this was part of a program what would be sensible initialisations to carry out?



Next: Exercises Up: The if-else Statement Previous: Multiple Choice Questions