The Nested if Statement


A nested if is an if statement that is the target of another if or else. Nested if statement means an if statement is inside another if statement. Yes, Java allows us to nest if statements within if statements; i.e., we can place an if statement inside another if statement.
if ( condition1 ) {
  // Executes when condition1 is true.
  if ( condition2 ) {
    // Executes when condition2 is true.
  }
}  


NestedIfDemo.java (the nested if statement)
// Java program to illustrate nested if statement 
class NestedIfDemo { 
  public static void main( String args[ ] ) { 
    int n1 = Integer.parseInt( args[0] );
    int n2 = Integer.parseInt( args[1] );
    if ( n1 != n2 ) {
      if ( n1 < n2 )
        System.out.print( true ); }
      else
        System.out.print( false );
  }
}
shell> java NestedIfDemo    

Output:           Result:




      I came up with a new word yesterday:    
      Plagiarism.