O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using else if

For cases when the simple binary condition test is not quite enough, an if statement can be refined with one or more else if clauses. These are similar to the else statement, except that they can also test for additional conditions. So, rather than having an A-B either-or type of test, you can add an additional dimension by testing a second condition.

Take a moment to consider the design implications of this mechanism; the two conditions do not need to be related in the same way that the if and else conditions are. In fact, the if condition in the else if statement can stand alone and test something entirely different.

The compiler and C language both allow this, but it can lead to quite convoluted coding if not used carefully. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required