Loading

What is If- else?

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:-

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:-

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:-

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:-

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