Nested Decision Structures
Frequently in programming, we need to answer a series of questions, or test a variable against one or more options. Nested decision structures are made for just that. When one or more decision structures are embedded within another, you have a nested decision structure. Remember, curly braces denote which pieces of code belong together. When nesting decision structures, it is essential you pay attention to your curly braces. This is the main reason why I comment ending curly braces.
The nested decision structures are interdependent. In other words, nothing stands alone. The top condition must reconcile to true in order for any of its nested expressions to even be evaluated.
Nested if Statements
When you need nested if statements and you are writing the 5-Step Solution Algorithm, you can code the structure as shown below. The scenario starts with choice1. Only if choice1 is "A" do we continue through the decision structure to choice2. If any of the choices prove to be false, (remember the condition is a Boolean expression), then the code jumps out of the decision structure. In other words, only if all the choices prove to be true does our really important task get executed. Pay attention to the ending curly braces.
IF (choice1 is "A"){
IF (choice2 is "2"){
IF (choice3 is "Blue"){
run the really important task code
}//end of choice3
}//end of choice2
}//end of choice1
The above nested if, statement should be reminiscent of the simple if statement in that we do not handle any scenarios where the condition is false.
Below is the Java code for a simple nested if statement. Pay attention to the data types! You should be able to determine the data types of each “choice”, just be reading through the code. If you do not understand the method .equalsIgnoreCase(), then you should review my unit on Library Methods and pay close attention to the section called Comparing Strings . Please study the code below.
if(choice1.equalsIgnoreCase("A")){
if(choice2 == 2){
if(choice3.equalsIgnoreCase("Blue")){
System.out.println("All choices were true,"
+ "it is time to do the really"
+ "important thing.");
}//end of choice3
}//end of choice2
}//end of choice1
A key take-away is that these nested if statements are interdependent. If even one of the individual conditions does NOT reconcile to true, then the entire decision structure is false. Also note, in the above example, we are not testing the same condition. Each if statement is testing a different condition.
Nested if/else Statements
When you need nested if/else statements and you are writing the 5-Step Solution Algorithm, you can code the structure as shown below. This code shows nested dual alternative decision structures, therefore, we handle the situations if a choice proves to be false. Please make sure you can match the else with the appropriate if
As you can see, if choice1 is not "A" we jump to the last else statement. Why the last one? Because the other if/else statements are nested inside of the outer statement (choice1).
IF (choice1 is "A") {
IF (choice2 is "2") {
IF (choice3 is "Blue"){
run the really important Blue task code
}//end of if for choice3
ELSE{
run the not so important task code
because choice3 was not "Blue"
}//end of else for choice3
}//end of if for choice2
ELSE{
We are here because choice2 is not "2".
We will do something else instead.
}//end of else for choice2
}//end of if for choice1
ELSE{
choice1 is not "A"
}//end of else for choice1
Nested if/else decision statements can be confusing. But, sometimes end-user requirements leave you no choice but to use a nested if/else. Typically, you create a nested decision statement when one value dictates what will happen next. Check out the Java code below. Nothing runs unless the value for state is WI. This scenario happens more than you may think. For instance, if the end-user is completing a form where the name field is required, you would not want to process the form UNLESS the name field contained text. In other words, only IF the name is not empty, do you continue processing.
Below is the Java code for the nested decision statements using actual data for the “choices”. At the very least, study the code below. However, I also recommend you type it into our development environment AND make the code work. I recommend adding comments throughout the code and on the ending curly braces.
// Nested decision structures
private void testingMultipleConditions(){
// Variables
String city = "River Falls";
String county = "Pierce";
String state = "WI";
Scanner input = new Scanner(System.in);
System.out.println("Which state do you live in?");
state = input.next();
if (state.equalsIgnoreCase("WI")) { //choice1
System.out.println("Which county do you live in?");
county = input.next();
if (county.equalsIgnoreCase("Dane")) { //choice2
System.out.println("Which city do you live in?");
city = input.next();
if (city.equalsIgnoreCase("Madison")) { //choice3
System.out.println("Madison is a big city");
}//end if city is Madison
else { //else for choice3
System.out.println("There are other cities in Dane County");
}//end of else for Madison/choice3
}//end if Dane county/choice2
else{ //else for choice2
System.out.println("Which city do you live in?");
city = input.next();
//bonus if/else nested inside the else for choice2
if (city.equalsIgnoreCase("Milwaukee")) {
System.out.println("That's too big for me");
}//end if city is Milwaukee
else {
System.out.println("I like" + " " + city);
}//end of else for Milwaukee
}//end of else for Dane country
} //end if state is WI/choice1
else { //else for choice1
System.out.println("Oh, you are not from WI, Goodbye");
}//end of decision structure
}//end testingMultipleConditions()
Interactive Live Demo
Below is an interactive demonstration of nested decision structures. This demo will accept either upper or lower case values for the state. However, in order for the state condition to reconcile to true, you must input the two character postal abbreviation for Wisconsin instead of spelling it out. What would your test plan be for this code? Remember to try the demo with other states.
What's next?
The next chapter in this unit is The switch Structure.