Java If Else

Sometimes we want to drink coffee when we feel sleepy. Sometimes, we order junk food if it is our friend’s birthday. You might want to buy an umbrella if it’s raining. All these decisions depend on a certain condition being met. Similar to real life, we can execute some instructions only when a condition is met in programming also. If-else block is used to check conditions and execute a particular section of code for a specific condition.

Flow control of if-else in Java :
Java If ELse

Decision-making instructions in Java

  • If-Else Statement
  • Switch Statement

If-Else Statement

The Java if-else statement tests the condition. It executes the if block if condition is true otherwise else block is executed.

Syntax of If-else statement in Java :

/* if (condition-to-be-checked) {
	statements-if-condition-true;
}
else {
	statements-if-condition-false;
} */

Example:
int a = 29;
if (a>18) {
	System.out.println(“You can drive”);
}
else{
       System.out.println(“You are underage!");
}

Output:
You can drive

If-else ladder : 

  • Instead of using multiple if statements, we can also use else if along with if thus forming an if-else-if-else ladder.
  • Using such kind of logic reduces indents.
  • Last else is executed only if all the conditions fail.
/* if (condition1) {

            //Statements;

else if {

            // Statements;

}

else {

            //Statements

} */

Note that the else block is optional.

Was this article helpful?
YesNo

Leave a Comment

Your email address will not be published. Required fields are marked *