5.6.1. Making Decisions

Statements and expressions for making decisions, or choices, are similar to those in Java, with a few small differences.

If-statements

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.

Omitting the else-part

If you omit the word else and the compound statement that follows it, 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.

Multiway tests

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

Details on if-statements

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

The condition is not required (by C++) to be a boolean value. It can be an integer or anything that is represented internally as an integer, such as a character or a member of an enumerated type or a pointer. In all cases, 0 means false and anything other than 0 means true.

However, for this course the coding standards require (with very minor exceptions) that conditions be boolean values. The condition should not be a pointer, for example.

The statements following the condition and following else are shown as compound statements (enclosed in braces) above. C++ does not require that. You can write any single statement at all. However, the coding standards for this course do require a compound statement.


Watch out: equality tests

If you are doing an equality test, be sure to use ==, not =. Statement
  if(n = 0)
  {
     m = 1;
  }
  else {
     m = 2;
  }
always makes m = 2. Expression n = 0 is not a test, it is an assignment. It stores 0 into n, and yields result 0. Since 0 is treated as false, the else-part is performed.

If you ask the compiler to give common warnings (option -Wall), it will let you know about this mistake.

Do not use = in a test even if it is what you want. The coding standards do not allow that.


Watch out: semicolons

Be careful about semicolons. A lone semicolon is a statement that does nothing. Statement
  if(n == 0);
  {
    printf("n is zero\n");
  }  
always writes "n is zero", regardless of the value of n. The semicolon at the end of the first line is an empty statement, so the if-statement says, if n equals 0, do nothing. The statement after that is just a compound statement. This really does two statements in a row.

The coding standards require you not to use a semicolon as an empty statement.


Making choices in expressions (optional)

Expression a ? b : c starts by evaluating expression a. If a is true (or nonzero), then it yields the value of expression b (and does not compute c at all). If a is false (or 0), it yields the value of expression c (and does not compute b at all). For example,
  int m = x > 0 ? x : -x;
creates variable m and stores the absolute value of x into m.

Only use this for expressions, not for statements. The coding standards require that.



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. The coding standards forbid the following.

      if(n == 0){}
      else
      {
        z = 1;
      }
    
    Rewrite that into an acceptable form that does the same thing. Answer

  3. The coding standards disallow the following. Explain what is going on and how to avoid this.

      if(x == 0)
      {
        y = 1;
      }
      else if(x != 0)
      {
        y = 2;
      }
    
    Answer

  4. 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