Program Logic

Unit 6 Index

Learning Multiple Decision Structures

Multiple Alternative Decision Structure

In programming, we may need to branch our code in more than an either/or direction. In other words, there could be more than one acceptable answer and it is up to us to write the code to capture and act upon the answer.

We use frequently if /else if/ else statements when we need to test one variable against many different values and do something different for each value.

Testing One Variable for Different Values

When you have a multiple alternative decision to make and you are writing the 5-Step Solution Algorithm, you can code the multiple alternative decision structure as shown below, where “true condition” is fully stated and run code in all situations is fleshed-out.

IF (true condition "A") {
  run the A is true code
}
ELSE IF (true condition "B"){ 
  run the B is true code
}
ELSE{ 
  run all above conditions are false code
}

Please study the Java code example below. Notice that just like in the dual alternative decision structure, the else statement does not have a condition associated with it. Remember, the else means, if all else fails, run this code.


// One variable, several choices

if (choice.equalsIgnoreCase("A")) {
  System.out.println("The choice was A");
}
else if (choice.equalsIgnoreCase("B")) {
  System.out.println("The choice was B");
}
else {
  System.out.println("The choice was none of the above");
}

The Flow of the Code

Decision structures are fundamental concepts in computer programming and it is vital you understand how they work. Since condition “A” is the if condition, it is always evaluated and if “A” evaluates to true, then no other condition in the above structure is evaluated. Period. If “A” is true, the code between the curly braces for “A” will execute then the code jumps completely out of the decision structure. Remember, the code only runs if the condition reconciles to true. Therefore, if “A” is false and condition “B” is true, then condition “A”, although evaluated, is skipped and the code between the curly braces for condition “B” runs. If both “A” and “B” evaluate to false then and only then does the code between the curly braces of the else statement runs.

One important take-away in this section is that whenever you use a multiple alternative decision statement ALWAYS include the else. Remember, it means, “if none of the above conditions are true, run the else code”.

Interactive Live Demo

Below is an interactive demonstration of the multiple alternative decision structure code. Test the code by entering the letter “A”, no quotes using either uppercase or lowercase, and then click the Run Code button. Do the same for the letter “B” and then run a third time with any other character. Notice the output for each of your entries. Compared to the code on this page, I altered the output for the else statement, but the concept is the same.





Limit Your else if Statements

Did you notice in the sample code on this page, there are only three choice, “A”, “B” and all the rest? It is possible to have more than one else if in a multiple alternative decision structure. However, multiple else if statements make your code harder to read and your code becomes less efficient. One rule of thumb adopted by many developers is that if you have more than one, max two, else if statements, then use a switch structure instead. We study switch structures later in this unit

Detailed Example

Here is a detailed example of using the Multiple Alternative Decision Statement. This example contains the 5-Step Solution Algorithm including the Java code. I highly recommend that you create a NetBeans project, type the Java code into it, add the comments, and test the program according to the test plan.

What's next?

After carefully reviewing the detailed example linked above, go to the chapter on Multiple Boolean Expressions..