Nested "if-then-else" Statements

This section describes nested 'if-then-else' statements, where an 'if-then-else' statement contains another 'if-then-else' statement.

In certain situations, the second contained sub-statement of an "if-then-else" statement needs to be another "if-then-else" statement. The result is a nested "if-then-else" statement.

Some times, you may have many "if-then-else" statements nested. Here is an example of 5 "if-then-else" statements nested together.

if (...) contained_statement // 1st "if" statement
else if (...) contained_statement // 2st "if" statement
     else if (...) contained_statement // 3rd "if" statement
          else if (...) contained_statement // 4th "if" statement
               else if (...) contained_statement // 5th "if" statement
                    else contained_statement

Note that a nested "if-then-else" statement can be written without indentations to indicate its nesting levels. But you need to remember that it is still a statement of 5 "if-then-else" statements nested together. It is not a single "if" statement with multiple contained sub-statements, which is not supported in Java.

if (...) contained_statement      // 1st "if" statement
else if (...) contained_statement // 2st "if" statement
else if (...) contained_statement // 3rd "if" statement
else if (...) contained_statement // 4th "if" statement
else if (...) contained_statement // 5th "if" statement
else contained_statement

The above statement is actually equivalent to the following from syntax structure point of view.

if (...) contained_statement      // 1st "if" statement
else { if (...) contained_statement // 2st "if" statement
else { if (...) contained_statement // 3rd "if" statement
else { if (...) contained_statement // 4th "if" statement
else { if (...) contained_statement // 5th "if" statement
else contained_statement } } } }

Here is a sample program that shows you how to use nested "if-then-else" statements:

/* NestedIfThenElseStatementTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.time.LocalTime;
class NestedIfThenElseStatementTest {
   public static void main(String[] arg) {
      java.io.PrintStream out = System.out;
      LocalTime now = LocalTime.now();
      int hour = now.getHour(); // from 0 to 23

      out.println("5 \"if-then-else\" statements nested together:");
      if (hour >= 6 && hour < 12)
         out.println("   Good morning!");
      else if (hour >= 12 && hour < 18)
              out.println("   Good afternoon!");
           else if (hour >= 18 && hour < 22)
                   out.println("   Good evening!");
                else if (hour >= 22 && hour < 24)
                        out.println("   Good night!");
                     else if (hour >= 0 && hour < 6)
                             out.println("   Why are you not sleeping?");
                          else
                             out.println("   Something wrong with clock!");
      out.println("   Current time is "+now);
   }
}

If you compile and run the above program, you will see:

herong> java NestedIfThenElseStatementTest.java

5 "if-then-else" statements nested together:
   Good afternoon!
   Current time is 14:29:50.479207

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

Control Flow Statements

 What Is Control Flow Statement

 "if-then" Statements

 "if-then-else" Statements

Nested "if-then-else" Statements

 "switch" Statements

 Fall-Through Behavior of "switch" Statements

 Basic "for" Statements

 Enhanced "for" Statements

 "while" Statements

 "do" Statements

 "break" Statements

 Labeled "break" Statements

 "continue" Statements

 Labeled "continue" Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB