Loading

It is a block of statements where if a condition is true the block of statements is executed otherwise it would not be executed.


Syntax:

if (conditions){
// if true this block will be executed
}
else{
// if false this will be executed
}


There are various types of if statements in Java

1. if-Statement: (Executes only if the condition is true)

Example:

package quipohouse;
public class IfStatement {
	public static void main(String[] args) {
		int a = 10, b = 20;
		if (a < b) {
			System.out.println("True");
		}
	}
}


Output:

True

2. if-else statement: (Executes one block if true, another if false)

Example:

package quipohouse;
public class IfElseStatement {
	public static void main(String[] args) {
		int a = 10, b = 20;
		if (a > b) {
			System.out.println("True");
		} else {
			System.out.println("False");
		}
	}
}


Output:

False

3. if-else-if ladder: (Checks multiple conditions)

Example:

package quipohouse;
public class IfElseIfStatement {
	public static void main(String[] args) {
		int a = 10, b = 20, c = 30;
		if (a > b) {
			System.out.println("True");
		} else if (b < c) {
			System.out.println("False");
		}
	}
}


Output:

False

4. Nested if statement: (if inside another if)

Example:

package quipohouse;
public class IfStatement {
	public static void main(String[] args) {
		int age = 61;
		if (age >= 18) {
			if (age >= 18 && age <= 60) {
				System.out.println("You are Eligible for Driving License");
			} else {
				System.out.println("You are Not Eligible for Driving                          License");
			}
		} else {
			System.out.println("You are Not Eligible to for Driving License");
		}
	}
}


Output:

You are Not Eligible for Driving License


Key Point
The if-else structure in Java helps control program flow based on conditions, making code more dynamic and responsive.