Using Else and Else If

The if conditional is useful in its own right, but it can be greatly expanded on by using the additional else and else if clauses. The syntax of the if-else conditional is

if (condition) {
   /* Do whatever. */
} else {
   /* Do this instead. */

Note that the else statements take place if the main condition is not true. In other words, the else acts as the default response (Figure 4.5).

Figure 4.5. This diagram shows how an application flows through an if-else conditional.

The else if clause takes this a step further by letting you set a secondary condition to check for if the initial one turns out to be false (Figure 4.6 ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.