Simple if statement
The simple if statement tells the program to execute a set of statements only if a particular condition evaluates to true.
Syntax
if (<condition>) { //code to be executed }
Example
In this program first, we are taking the users age, and then checking if the user is eligible to vote or not. If users age is greater than or equal to 18 then simply print “Eligible to vote”.
import java.util.Scanner; public class SimpleIfExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter your age : "); int age = scanner.nextInt(); // Simple if statement if (age >= 18) { System.out.println("Eligible to vote"); } } }
Flow diagram
Flow-diagram of the above example program –