Answer to Question 07B-5

(a) Yes, that is allowed in C++.

(b) No, that is not allowed by the coding standards for this course. You are required to use compound statements, as in the following.

  if(x > 0)
  {
    y = 0;
  }
  else
  {
    y = x - 1;
  }

One reason for that concerns what happens when you modify a program. Suppose you start with

  if(m == n)
    y = 1;
where there is no else part. Then you decide to add another statement, to be done only when m and n are equal. After modification, you have
  if(m == n)
    printf("m = n, so I am setting y = 1.\n");
    y = 1;
But the body of an if-statement is one statement. So statement y = 1; is done regardless of whether m and n are equal. The indentation is misleading. Requiring braces takes away the opportunity to make this type of error.