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

The else Keyword

You could construct all your condition testing in programs with a series of compound if statements—after all, if you can test for one condition to be true, you can also test for it to be false and do something else. The if statement provides a binary condition test: The result is either true (1) or false (0). Based upon the result of the evaluation, the code that follows the condition is executed or not.

It is helpful, however, to have the possibility to selectively execute code as an alternative without having to create a specific condition for it. In other words, you would like to have the equivalent of the plain English:

“If it is raining then I will take an umbrella; otherwise, I will take sunglasses.”

You can state this ...

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