In this tutorial you will learn about the Java if else statement and its application with practical example.
Java if-else statement
In Java, when we want to execute a block of code when if the condition is true and another block of code when if the condition is false, In such a case we use the if-else statement.
Java If…else Statement Flow Diagram
Syntax:-
1 2 3 4 5 |
if(condition){ // statements } else { // statements } |
Here, Condition is a Boolean expression that results in either True or False, if it results in True then statements inside if body are executed, if it results in False then statements inside else body are executed.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Main { public static void main(String[] args) { int a = 10; int b = 20; System.out.println("W3Adda - Java If else Statement"); if(a > b){ System.out.println("a is greater than b"); } else { System.out.println("b is greater than a"); } } } |
Output:-