Java


Java Tutorial


Admission Enquiry Form

  

If Else Statement in Java

What is if else statement in Java?

If else statement is conditional statement in Java. It is used to check the condtion.It executes if block when the condition is true, otherwise else block will be executed.

Syntax of if else statement...

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




Example of if else statement in Java:

//Java Program of if else statement.
public class IfElseStatement {
public static void main(String[] args) {

int num=4; //initializing a variable.

if(num>5) //checking the condition.
{
System.out.println("Welcome to Compuhelp");
}//end of if block
else
{
System.out.println("Thanks to join Compuhelp");
}// end of else block
} //end of main method

}//end of class IfElseStatement


Output

Thanks to join Compuhelp.

Description:

In the above example, the else block will get excecuted because, the condition given in the if statement is will be false because, the variable num containing value less than 5.