Java Conditionals: If-else Statement in Java
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 :

Decision-making instructions in Java
- If-Else Statement
- Switch Statement
If-Else Statement
Syntax of If-else statement in Java :
Example:
Output:
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.
Note that the else block is optional.
Handwritten Notes: Click to Download
Ultimate Java Cheatsheet: Click To Download