Some questions on conditional statements

  1. What is wrong with each of the following statements?
    1. if x==4 y++;
      Missing parentheses around the comparison. Should be
      if(x==4) y++;
    2. if(x=4) ++y;
      Assignment instead of comparison. Should be
      if(x==4) y++;
    3. if (x==4) ++y else --y;
      Missing missing semi-colon at the end of ++y. Should be
      if(x==4) ++y; else --y;
      Remember that the semi-colon is a statement terminator, not a statement separator.

  2. Write a program which reads in two integer numbers and writes out the larger of the two.
    You may want to refer ahead to find out how to read innumbers or write them out.
    FInd a sample answer here.
    ¥

Back to the questions.


Back to the notes on conditionals.