### Nested conditional statements

• Previously discussed

• Conditional statements discussed so far:

 if-statement if-else-statement

• Syntax of the if-statement:

 ``` if ( CONDITION ) ONE-statement ```

• Syntax of the if-else-statement:

 ``` if ( CONDITION ) ONE-statement else ONE-statement ```

• Nested conditional statements

• Fact:

 A conditional statement (i.e., an if-statement or an if-else-statement) is also a statement

Therefore:

 We can use an if-statement or an if-else-statement in the then-part (and in the else-part) of a conditional statement !!!

• Nested conditional statement:

 Nested conditional statement = a conditional statement where the then-part and/or the else-part contains another conditional statement

• Note:

• You can nest conditional statement arbitrarily deep

• Obviously, deeper nesting makes the program difficult to understand

• You should:

 Use nested conditional statements only when necessary Try not to nest conditional statements too deeply

• Programming example: determine the price for a hair cut                            (board)

• Hair cut pricing of a saloon:

 Male customer: Boys (age ≤ 13): \$10 Men (age > 13): \$15 Female customer: Girls (age ≤ 13): \$12 Women (age > 13): \$25

Write a program that:

 Reads in the sex and the age Prints the price of the hair cut.

• Algorithm:

• Java program:

 ``` public class NestedIf01 { public static void main(String[] args) { char sex; int age, price = 0; Scanner in = new Scanner(System.in); // Construct Scanner object sex = in.next().charAt(0); // Read in next char into sex age = in.nextInt(); // Read in next integer into age if ( sex == 'M' ) { // Then-part of outer if-else statement if ( age <= 13 ) { price = 10; // Case: boy <-- then-part of inner if-else } else { price = 15; // Case: man <-- else-part of inner if-else } } else { // Else-part of outer if-else statement if ( age <= 13 ) { price = 12; // Case: girl <-- then-part of inner if-else } else { price = 25; // Case: woman <-- else-part of inner if-else } } System.out.println("Price = " + price); } } ```

Explanation:

• Suppose the user enters: sex = 'M' and age = 11

• Then the condition of the outer if-else-statement is satisfied and executes only the then-part of the outer if-else-statement:

 ``` // Then-part of outer if-else statement if ( age <= 13 ) { price = 10; // Case: boy <-- then-part of inner if-else } else { price = 15; // Case: man <-- else-part of inner if-else } ```

• Since the then-part of the outer if-else-statement is an if-else-statement, and the condition age <= 13 is satisfied, it will only execute the then-part:

 ``` price = 10; ```

Graphically: path taken by program when input is sex = 'M' (male) and age = 11

Notice that only the statement(s) in then-part of the inner and outer if-else-statements are executed (because the conditions in the inner and outer if-else-statements are satisfied)

• Another example: path taken by program when input is sex = 'F' (female) and age = 25

Note:

 The expression in.next() reads in a string from the keyboard The expression in.next().charAt(0) returns the first character in a string from read from the keyboard

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac NestedIf01.java To run:          java NestedIf01

• Programming example: assign a letter grade to a number grade

• Letter grade assignment:

 grade ≥ 90:             A               80 ≤ grade < 90:     B 70 ≤ grade < 80:     C 60 ≤ grade < 70:     D grade < 60:             F

Write a program that:

• Algorithm using non-nested if-statements:

• Java program:

 ``` import java.util.Scanner; public class Grade01 { public static void main(String[] args) { double ng; String lg = ""; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter number grade: "); ng = in.nextDouble(); // Read in next number into ng if ( ng >= 90 ) lg = "A"; if ( 80 <= ng && ng < 90 ) lg = "B"; if ( 70 <= ng && ng < 80 ) lg = "C"; if ( 60 <= ng && ng < 70 ) lg = "D"; if ( ng < 60 ) lg = "F"; System.out.println("Letter grade = " + lg); } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Grade01.java To run:          java Grade01

• Short-coming of the algorithm:

• It is inefficient

Because the algorithm will always perform 5 individual tests (in the 5 different if-statements

• Fact:

• The different cases are mutually disjoint

E.g., when the first condition passes (i.e., grade ≥ 90), then the other 4 subsequent tests will all fail

The algorithm will still have to go through these "useless" tests to reach the print statement

• We can make use of the if-else-statement to shorten the execution

• Improved algorithm to find numeric grade

• Improved algorithm to assign a letter grade:

Explanation:

• The first if-else statement breaks the cases into: ng ≥ 90 and ng < 90:

If ng ≥ 90, we can assign lg = "A";

 No further tests will be performed !!! (I.e.: much faster !!!)

Otherwise, the execution will enter the else-part

• The second if-else statement knows that ng < 90 !!!

It uses the test ng >= 80 to break the cases into: 80 ≤ ng < 90 and ng < 80:

Because we know that ng < 90 when we are inside the else-part of the first if-else statement, when ng ≥ 80, we can assign lg = "B"; because 80 < ng ≤ 90

 No further tests will be performed !!! (I.e.: again, much faster !!!)

Otherwise, the execution will enter the else-part of the second if-else statement

Inside the else-part of the second if-else statement, we know (for sure) that ng < 80.

• The rest of the algorithm follows the same reasoning process...

• Program in Java: (I omitted the import statement for brevity)

 ``` public class Grade02 { public static void main(String[] args) { double ng; String lg = ""; Scanner in = new Scanner(System.in); // Construct Scanner object ng = in.nextDouble(); // Read in next number into ng if ( ng >= 90 ) lg = "A"; else { if ( ng >= 80 ) lg = "B"; else { if ( ng >= 70 ) lg = "C"; else { if ( ng >= 60 ) lg = "D"; else lg = "F"; } } } System.out.println("Letter grade = " + lg); } } ```

• Two-way selection

• A two-way selection is a choice between 2 mutually exclusive cases:

• The if-else-statement is known as a two-way selection statement because it can use to make a choice between 2 mutually exclusive cases:

• A three-way selection construct

A two-way selection is a choice between 3 mutually exclusive cases:

The choices of A, B and C are mutually exclusive.

• A common way to ensure that the 3 choices are mutually exclusive is as follows:

• The 3-way selection construct can be implemented using the following nested if-else statements:

Notes:

 The statement(s) S1; are executed only when condition1 = true The statement(s) S2; are executed only when (condition1 = false and condition2 = true) The statement(s) S3; are executed only when (condition1 = false and condition2 = false)

Each of the conditions are mutually exclusive

• The 3-way selection written in Java:

 ``` if ( conditionX ) { statements executed only when: conditionX = true } else { if ( conditionY ) --+ { | statements executed only when: | conditionX = false | and conditionY = true | } | One statement!! else | { | statements executed only when: | conditionX = false | and conditionY = false | } --+ } ```

Since there is one statement in the else-part, we do not need to use a block

• The popular way to write a 3-way selection in Java:

 ``` if ( conditionX ) { statements executed only when: conditionX = true } else if ( conditionY ) --+ { | statements executed only when: | conditionX = false | and conditionY = true | } | One statement!! else | { | statements executed only when: | conditionX = false | and conditionY = false | } --+ ```

• The N-way selection construct

• The 3-way selection construct can be generalized into an N-way selection

A N-way selection construct looks like the following:

 ``` if ( condition1 ) { S1; (one or more statements) } else if ( condition2 ) { S2; (one or more statements) } else if ( condition3 ) { S3; (one or more statements) } ... else if ( conditionN-1 ) { SN-1; (one or more statements) } else { SN; (one or more statements) } ```

Notes:

 S1 will be executed (only) when condition1 = true S2 will be executed (only) when (condition1 = false and condition2 = true) S3 will be executed (only) when (condition1 = false, condition2 = false and condition3 = true) And so on...

• Example: the number grade program can be written using an N-way selection

 ``` public class Grade03 { public static void main(String[] args) { double ng; String lg = ""; Scanner in = new Scanner(System.in); // Construct Scanner object ng = in.nextDouble(); // Read in next number into ng if ( ng >= 90 ) { lg = "A"; } else if ( ng >= 80 ) { lg = "B"; } else if ( ng >= 70 ) { lg = "C"; } else if ( ng >= 60 ) { lg = "D"; } else { lg = "F"; } System.out.println("Letter grade = " + lg); } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Grade03.java To run:          java Grade03