9A. Making Decisions


If-statements

Statements for making decisions, or choices, are similar to those in Java.

if-else

Statement

  if (condition)
  {
    statements1
  }
  else
  {
    statements2
  }  
starts by evaluating condition, an expression that normally yields either true or false. If the condition yields true, then statements1 are performed. If the condition yields false, then statements2 are performed. For example,
  if(x > 0)
  {
    y = x;
  }
  else
  {
    y = -x;
  }
ends with y equal to the absolute value of x.

We will call statements1 the true-body of the if-statement and statements2 the false-body.

The parentheses around the condition in an if-statement are required. Do not omit them.


Example: finding the larger of two numbers, version 1

If-statements are useful for breaking a problem down into cases. Here is an example, finding the maximum of two values x and y, having a case where x > y and a case where xy.
   // maximum(x,y) returns the larger of x and y.
   // If x and y are equal, it returns that shared value.
   // Examples:
   // maximum(1,5) = 5
   // maximum(3,2) = 2
   // maximum(4,4) = 4

   int maximum(const int x, const int y)
   {
     if(x > y)
     {
       return x;
     }
     else
     {
       return y;
     }
   }

Omitting the else part

If you omit the word else and the false-body, then nothing is done when the condition is false. For example,
  if(x < 0)
  {
    x = -x;
  }
will always end with x ≥ 0, since it does nothing if x ≥ 0 when it starts.

If you want one of the bodies of an if-statement to do nothing, make it be the false-body, and omit else. The standards require that.


Example: finding the larger of two numbers, version 2

// maximum(x,y) returns the larger of x and y.
// If x and y are equal, it returns that shared value.
// Examples:
// maximum(1,5) = 5
// maximum(3,2) = 2
// maximum(4,4) = 4

int maximum(const int x, const int y)
{
  int result = x;
  if(x < y)
  {
     result = y;
  }
  return result;
}

More than two cases

If you have more than two cases, use the following indentation scheme.
  if(test1)
  {
    …
  }
  else if(test2)
  {
    …
  }
  else if(test3)
  {
    …
  }
  else
  {
    …
  }

Example: Getting the sign of a number

// sign(x) is
//    -1   if x < 0
//     1   if x > 0
//     0   if x = 0

int sign(const double x)
{
  if(x < 0)
  {
    return -1;
  }
  else if(x > 0)
  {
    return 1
  }
  else
  {
    return 0;
  }
}

Are the bodies required to be compound statements?

According to the rules of C++, the true-body and the false-body can be any single statement. For example, the definition of maximum can be written as
   int maximum(const int x, const int y)
   {
     if(x > y)
       return x;
     else
       return y;
   }
or even
   int maximum(const int x, const int y)
   {
     if(x > y) return x;
     else return y;
   }
and those forms will be useful during lectures, where space is limited. But the standards for this course require that the true-body and false-body must be compound statements.

One reason behind the standard 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 kind of error.



Exercises

  1. Assume that integer variable n already has a value. Write a statement that makes m equal to 3n+1 if n is odd and to n/2 if n is even. Answer

  2. Suppose that you want to set y equal to

    Explain why the following does not do the job.

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

    Answer

  3. Are the parentheses around the condition in an if-statement required or optional? Answer

  4. Is the condition in an if-statement required to use a comparison operator? Answer

  5. Is the following allowed (a) in C++, (b) by the coding standards for this course?

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