O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

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

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