AP Computer Science Java: Lesson 2.6
Conditional Statements - The If..Else..If Statement

Lesson 2.6 - The If..Else..If Statement

Purpose: To learn how to combine several related if statements into one if..else..if statement

Multiple Choices
Let's begin this discussion with an example programming problem:

Suppose that a program is being written that will display the word version of a two-digit integer between 20 and 59. For example, the integer 32 will be displayed in words as thirty-two. The following code could be used to write the part that will appear before the hyphen (assume that n is an int variable):

   if(n>=20 && n<30) System.out.println("Twenty");
   if(n>=30 && n<40) System.out.println("Thirty");
   if(n>=40 && n<50) System.out.println("Forty");
   if(n>=50 && n<60) System.out.println("Fifty");

While this performs the intended task, it is actually quite inefficient. The reason is that the computer will check every condition even though only one of them can be true. For example, suppose that n = 28. The first condition is true, so the computer displays Twenty. Now the computer checks all of the other conditions which are all false (and cannot possibly be true at this point). Checking conditions slows the computer down. While this may not be a big deal in this small situation with only four if statements, imagine large, commercial software in which thousand and millions of decisions are being made. The time checking false conditionals would be excessive.

So how can we improve upon the previous code? By combining all of the if statements into one long statement called an if..else..if statement. Here is what it would look like:

   if(n>=20 && n<30) System.out.println("Twenty")
   else if(n>=30 && n<40) System.out.println("Thirty")
        else if(n>=40 && n<50) System.out.println("Forty")
             else if(n>=50 && n<60) System.out.println("Fifty")

Take note of several differences. First, the similarity to an if..else, with the word if appearing after each else checking a new condition. Second, there is only one semi-colon, located at the end. That is because this is actually one statement, which takes the place of the previous four statements. Lastly, there is indentation which is not required but used to make the statement easier to read. The most important to know about this statement is why it is more efficient. Since it is one statement, it will only perform one display statement - the one with the true condition. So now when n = 28, the first condition is true, the computer displays Twenty and because the if is followed by an else, the compute skips out of the statement and moves on to the next line of code following the if..else..if.

One more thing If the value of n was outside the range of 20-59, nothing will happen. For instance, the value 75 does not make any of the conditions true, so nothing is displayed. If we can be sure that n will not be outside the range 20-59, we could write the code as follows:

   if(n>=20 && n<30) System.out.println("Twenty")
   else if(n>=30 && n<40) System.out.println("Thirty")
        else if(n>=40 && n<50) System.out.println("Forty")
             else System.out.println("Fifty")

Note that in this version, the last condition is omitted. Since it is the only possibility at that point, the program just displays Fifty without needing to check a condition - it has to be a n in the 50's at tha point. The last else then could be considered a default response - if none of the previous conditions are true, just do this.

In closing, use an if..else..if only when you are coding 3 or more conditional statements that are all based on the same variable or expression. For example, the following could not be converted to an if..else..if:

   if(x==1) System.out.println("One");
   if(y==2) System.out.println("Two");
   if(z==3) System.out.println("Three");

More than one of these conditions can be true, since they are based on different variables. So an if.else..if would not make sense.

© DanShuster.com