A Multi-Way If Statement

Let's change our program once again to take into account the coefficient of friction. You can see a version of our block program that solves this problem by reading ``examples/block3.c'' into Emacs. You can also view it directly.

This program contains an if statement that must choose from among three alternatives. The general form of a multi-way if statement is:

  if ( condition-1 ) {
      ...
  }
  else if ( condition-2 ) {
      ...
  }
  else if ( condition-3 ) {
      ...
  }
  else {
      ...  
  }

You can use as many else if lines as you need, and the final else is optional. Thus, we can rewrite the code fragment above as


Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah