Java


Java Tutorial


Admission Enquiry Form

  

If Else If Statement in Java


What is if else if statement in Java?

If else if statement is conditional statement in Java. It is used to check the condtion.It executes one block out of multiple blocks.

Syntax of if else if statement...

if(condition)
{
//number of statements
}
else if(condition)
{
//number of statements
}




Example of if else if statement in Java:

//Java Program of if else if statement.
public class IfElseIfStatement
{
public static void main(String[] args)
{
int num=4; //initializing a variable.
if(num==0) //checking the condition.
{
System.out.println("It is zero");
}//end of if block.
else if(num>0)
{
System.out.println("It is positive");
}// end of first else if block.
else if(num<0)
{
System.out.println("It is positive");
}//end of second else if block.
} //end of main method
}//end of class IfElseIfStatement


Output:

It is positive.

Description:

In the above example, only first else if block will get excecuted because, the condition given in first else if block will be true.