# Multiple Alternatives: Nested if statement

• Introduction

• The if statement can become pretty complicated when we use another if statement inside an if statement.

• The if statement revisited

• Recall the syntax of the if statement:

 ``` if ( Condition ) ONE-single-statement ```

• And recall the syntax of the if-else statement:

 ``` if ( Condition ) ONE-single-statement1 // "then part" else ONE-single-statement2 // "else part" ```

• Recall what constitute ONE single statement:

1. ONE assignment statement
2. ONE if statement
3. ONE if-else statement
4. ONE compound statement

• If you put these fact together, then you come to the conclusion that you can use an if statement for the ONE single statement

• Example of a nested if statement

• In the following example, we use an if-else statement in the "then part" of the if-else statement:

 ``` if ( Condition ) ONE-single-statement1 else ONE-single-statement2 ``` ``` if ( Condition ) if ( Condition2 ) ONE-single-statement1a else ONE-single-statement1b else ONE-single-statement2 ```

• The meaning of the Nested if statement or Nested if-else statement is UNCHANGED - you just have to apply it CONSISTENTLY

• See here for the meaning of the if statement: click here

• See here for the meaning of the if-else statement: click here

• Example:

In many countries in Europe, males of age 18 or more are drafted for military service. The following construct can be use to check for military draft eligibility

 ``` String sex; // Assume sex is given one of the values "M" or "F" int age; // Assume age is given some value > 0 String draft; // Hold the decision on draft eligibility .... // (indicates rest of program left off // I can't show everything all the time !!!) if ( sex.equals("M") ) if ( age >= 18 ) draft = "Yes"; else draft = "No"; // (A) else draft = "No"; // (B) ```

• The outer if-else statement is executed first.

• If the condition (sex.equals("M")) is satisfied, then the sex is "M", and the "then part" statement is executed.

• The "then part" statement is another if-else statement and it is executed by testing the condition (age >= 18)

• If this condition (age >= 18) is satisfied, then both conditions (sex.equals("M") and age >= 18) are met; and draft decision draft is set to "Yes".

• But if the second condition (age >= 18) is NOT satisfied, then the "else part" statement (marked with (A)) is executed and the draft decision draft is set to "No".

• If the first condition (sex.equals("M")) is NOT satisfied, then the sex is "F", and the "else part" statement is executed. This statement is marked with (B) and sets the draft decision draft to "No".

• Example Program: (Demo above code)

• Multiple Alternatives: Sequences of Comparisons

• A very common form of nested if-else statement is "tail" nesting:

 ``` if ( Condition1 ) ONE-single-statement1 else if-statement2 ``` ``` if ( Condition1 ) ONE-single-statement1 else if ( Condition2 ) ONE-single-statement2a else ONE-single-statement2b ```

• When the statement in the "else" is another IF-statement, the if-statement is generally written as follows:

How the program is read: How the program is commonly written:
```
if ( Condition1 )
ONE-single-statement1
else
if ( Condition2 )
ONE-single-statement2a
else
ONE-single-statement2b
```
```
if ( Condition1 )
ONE-single-statement1
else if ( Condition2 )
ONE-single-statement2a
else
ONE-single-statement2b

```

• This nested if-statement is executed as follows:

• If Condition1 is satisfied, then ONE-single-statement1 is executed.

• Otherwise, if Condition2 is satisfied, then ONE-single-statement2a is executed.

• Otherwise (every conditions specified failed), ONE-single-statement2b is executed.

• This nested-if construct can be used to formulate multiple alternative and the first matching condition is executed

IMPORTANT

• The general way to express a multiway alternative is as follows:

 ``` if ( Condition1 ) ONE-single-statement1 else if ( Condition2 ) ONE-single-statement2 else if ( Condition3 ) ONE-single-statement3 .... else if ( Conditionn ) ONE-single-statementn else ONE-single-statementn+1 ```

• If the first matching condition is Conditioni , then ONE-single-statementi is executed (all other statements are not executed !)

• If every condition fails to match, then ONE-single-statementn+1 is executed (all other statements are not executed !)

• Example:

A numeric grade can be converted into a letter grade as follows:

• 90 - 100 ==> A
• 80 - 89.99.. ==> B
• 70 - 79.99.. ==> C
• 60 - 69.99.. ==> D
• 0 - 59.99.. ==> F

Here is a program that does this automatically:

 ``` public class Grade { public static void main(String[] args) { double avg; ... // avg is given some value if ( avg >= 90 ) System.out.println("Grade is A"); else if ( avg >= 80 ) System.out.println("Grade is B"); else if ( avg >= 70 ) System.out.println("Grade is C"); else if ( avg >= 60 ) System.out.println("Grade is D"); else System.out.println("Grade is F"); } } ```

• Example Program: (Demo above code)

• Self Check

• Will the following program assign the letter grades correctly using the numeric grade "avg" ?

 ``` public class Grade { public static void main(String[] args) { double avg; ... // avg is given some value if ( avg >= 60 ) System.out.println("Grade is D"); else if ( avg >= 70 ) System.out.println("Grade is C"); else if ( avg >= 80 ) System.out.println("Grade is B"); else if ( avg >= 90 ) System.out.println("Grade is A"); else System.out.println("Grade is F"); } } ```

NO ! Try avg = 95. The test "avg >= 60" succeeds, so "Grade is D" is printed. But the correct output for avg = 95 is "Grade is A".

The ordering of the test is very important !!!