In the if-else control statement. Code will be executed based on a condition. If the condition is true then the if block is executed and if the condition is false then the else block is executed.
Syntax :
if(condition) {
// if the condition is true then this block of code will execute
}
else {// if the condition is false then this block of code will execute
}
Flow Chart:
Example:
import java.util.Scanner;public class if_elsetest {/*** @param args*/public static void main(String[] args) {Scanner sc = new Scanner(System.in);int age = sc.nextInt();if(age>18){System.out.println("adult");}else{System.out.println("not adult");}}}
In this example, if age is grater than 18 then if block will exucuted ( output is adult ) other wise else block will exucuted ( output is not adult ).