Java nested if statement

In this chapter you will learn:

  1. What is Java nested if statement
  2. Example - Java nested if statement

Description

A nested if is an if statement inside another another if statement or else.

Example

The following code uses a nested if statement to compare values.

 
public class Main {
  public static void main(String[] argv) {
    int i = 10;/*from   w  w w . ja v  a 2s .  co  m*/
    int j = 4;
    int k = 200;
    int a = 3;
    int b = 5;
    int c = 0;
    int d =0;
    if (i == 10) {
      if (j < 20){
        a = b;
      }
      if (k > 100){
        c = d;
      }
      else{
        a = c;
      }
    } else{
      a = d;
    }
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);
    System.out.println("d = " + d);
    
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. What is the Java Switch Statement
  2. Syntax for Java switch Statement
  3. Example - Java Switch Statement
  4. Example - How to use the optional break statement
  5. Example - How to write nested switch Statements
Home »
  Java Tutorial »
    Java Langauge »
      Java Statement
Java if Statement
Java if else Statement
Java if else ladder statement
Java nested if statement
Java switch Statement
Java for loop
Java for each loop
Java while Loop
Java do while loop
Java break statement
Java continue statement
Java Comments
Java documentation comment(Javadoc)