1else if statement is used to specify new condition if first condition is false.
2Syntax:
3
4if(condition1)
5{
6 // execute if condition1 is true
7}
8else if (condition2)
9{
10 // execute if condition2 is true
11}
12else if (condition3)
13{
14 // execute if condition3 is true
15}
16else
17{
18 // execute if conditions 1, 2 and 3 becomes false
19}
1import java.io.*;
2public class JavaIfElse
3{
4 public static void main(String[] args)
5 {
6 int number = 15;
7 // check if number is divisible by 2
8 if(number%2 == 0)
9 {
10 System.out.println(number + " is even number");
11 }
12 else
13 {
14 System.out.println(number + " is odd number");
15 }
16 }
17}
1if(a<b){
2 //if a<b you are here
3 //do something
4}else if(a==b){
5 //if a==b you are here
6 //do something
7}else{
8 //if none above conditions are matched you are here
9 //do something
10}
1public class TestIfElse {
2
3 public static void main(String[] args) {
4 int a = 5, b = 6;
5 if (a > b) {
6 System.out.println("a is greater");
7 } else {
8 System.out.println("b is greater");
9 }
10 }
11}
1int x = 3;
2
3if (x == 3) {
4 // block of code to be executed if the condition is true
5} else {
6 // block of code to be executed if the condition is false
7}