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):

if (condition 1) {
   /* Do whatever. */
} else if (condition 2) {
   /* Do something else. */
}

Figure 4.6. An if-else if conditional can have multiple, separate conditions to test against.


You can have as many else if clauses as you want. You can even, and often do, combine else and else if clauses together, so long as the else is the final part of the conditional (because it's the default case). The following example rewrites the gpa.c application more professionally.

To use else and else if

1.
Open gpa.c (Script 4.2) in your text editor or IDE.

2.
Delete all of the existing conditionals (Script 4.3):

Script 4.3. By using else and else if clauses, gpa.c (Script 4.2) can be rewritten using one complex control structure.


The three separate if conditionals will be rewritten as one if-else if-else if-else conditional and is therefore no longer needed.

3.
Create the main conditional:

if (gpa >= 3.9) {
     printf ("You're graduating summa
 cum laude with a %0.2f GPA!
", gpa);
} else if (gpa >= 3.75) {
     printf ("You're graduating magna
 cum laude with a %0.2f GPA!
", gpa);
} else if (gpa >= 3.5) {
     printf ("You're graduating cum
 laude with a %0.2f GPA!
", gpa);
} else {
     printf ("There's more to life
 than grades.
");
}

The conditions themselves are similar to those in the previous version of this code, but now they have all been tied into one conditional. The application will continue through the conditional until a condition is met. If none of the if or else if conditions are true, the else statement will be run.

Notice that the second and third conditions do not need to check if a value is less than a certain value. For example, if gpa is equal to 3.8, the first condition is false and the second is true. In this second condition, you no longer need to check if gpa is less than 3.9 (as in the previous version of this application) as that's already been determined by the first condition. The same notion applies to the third condition (you already know that gpa is less than 3.75).

4.
Save the file as gpa2.c, compile, and run (Figure 4.7).

Figure 4.7. For the most part, the application behaves as it previously had (see Figure 4.3 and 4.4) even though the main conditional has been rewritten.


5.
Change the value of gpa, recompile, and rerun the application (Figure 4.8).

Figure 4.8. Now a response is printed if the grade point average is below 3.5.


✓ Tips

  • Although it's not required, indenting statements four spaces from their associated if, else, or else if clause (as in gpa2.c) makes it easier to see which statements are associated with which clauses.

  • Like a simple if statement, the if-else and if-else if don't require curly braces if each has only one resulting statement. Again, it's good practice to always use the curly braces, though.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset