There are two-part in an if-else statement. First is the if statement, here it checks for the condition to be true and if it is true then it will execute the code present inside the if-block. But if the condition is false then the code inside the else statement will be executed.
Flowchart for if-else statement in Java:
Syntax of an if-else statement:
1 2 3 4 5 6 7 8 | if (condition) { //code executed if condition true } else { //code executed if condition false } |
Example of an if-else statement in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class IfElseTest { public static void main(String args[]) { int a = 5; if (a < 10) { System.out.println("If statement executed"); } else { System.out.println("Else statement executed"); } } } |
Output:
1 | If statement executed |