if…else if Statement

An if proclamation can be trailed by a non-compulsory else if…else explanation, which is exceptionally helpful to test different conditions utilizing single if…else if articulation.

At the point when utilizing if , else if , else proclamations there are few focuses to remember.

  • An if can have zero or one else’s and it must come after any else if’s.

  • An if can have zero to numerous else if’s and they must precede the else.

  • If one of the if conditions yield a true, the other else ifs and else are ignored.

The syntax for using this decision making construct Is as follows:

if (condition_1) {
//Execute if condition_1 is true
} else if (condition_2) {
//Execute if condition_2 is true
} else if (condition_3) {
//Execute if condition_3 is true
} else {
//Execute if all conditions are false
}

Sample Implementation:

public class myTest {
  public static void main(string args[]) {
    int i = 0;
    if (i > 1) {
      System.out.print(“The first
        if construct is executing!”);
    } else if (i == 0) {
      System.out.print(“The second
        if construct is executing!”);
    } else {
      System.out.print(“The
        else construct is executing!”);
    }
  }
}

This would create the accompanying result: 

The second if construct is executing!