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 www. jav  a 2  s. c  o 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:





















Home »
  Java Tutorial »
    Java Language »




Java Data Type, Operator
Java Statement
Java Class
Java Array
Java Exception Handling
Java Annotations
Java Generics
Java Data Structures