else-if statement


The else if statement is used to execute the statements when condition specified in the above if statement is false.


Syntax:

if (condition 1)
{
    statements to be executed when condition 1 is true
}
else if (condition 2)
{
    statements to be executed when above if condition is false
}

Example:

int a = 15;

if(a < 10)
{
	System.out.print(“a is less than 10”);
}

else if(a < 20){
	System.out.print(“a is less than 20 but greater than 10”);
}

else
{
	System.out.print(“a is greater than 20”);
}

Output:

a is less than 20 but greater than 10