if-else statement in Java

The if statement is used to execute a set of statements based on the condition. The condition may evaluate to true or false. There are four ways in which you can use if statement in Java-

if statement in Java

A set of statements inside if block is executed when the condition evaluates to true. Otherwise, if block is skipped. If there is only one line in the if block then there is no need of braces.

Syntax

if(condition)
{
	//code to be executed
}

Flowchart of if statement

if statement in Java

Example

The below example will determine whether a number is divisible by 2 or not.

public class Example
{
	public static void main(String args[])
	{
		int a = 12;
		if(a%2 == 0)
		{
			System.out.println("a is divisible by 2");
		}
	}
}

Output

a is divisible by 2

if-else statement in Java

If a condition evaluates to true then if block will be executed. Otherwise, else block will be executed.

Syntax

if(condition)
{
	//code to be executed when condition is true
}else{
	//code to be executed when condition is false
}

Flowchart of if-else statement

if-else statement in Java

Example

The below example will determine a maximum of two numbers.

public class Example
{
	public static void main(String args[])
	{
		int a = 30;
		int b = 20;
		if(a>b)
		{
			System.out.println("a is greater than b");
		}else{
			System.out.println("b is greater than a");
		}
	}
}

Output

a is greater than b

if-else if statement in Java

In an if-else, if constructs the conditions are tested one by one starting from the top. If a condition returns true, the block of code following the condition is executed and all the following else if blocks will be skipped.

Syntax

if(condition1)
{
	//code to be executed if condition1 is true 
}
else if(condition2)
{
	//code to be executed if condition2 is true
}
else if(condition3)
{
	//code to be executed if condition3 is true
}
...
else
{
	//code to be executed when all the conditions are false
}

Flowchart of if-else if statement

if-else if statement in Java

Example

Following example determines a maximum of three numbers.

public class Example
{
	public static void main(String args[])
	{
		int a = 5;
		int b = 10;
		int c = 15;
		if(a>b && a>c)
		{
			System.out.println("a is the maximum");
		}
		else if(b>a && b>c)
		{
			System.out.println("b is the maximum");
		}
		else
		{
			System.out.println("c is the maximum");
		}
	}
}

Output

c is the maximum

nested if statement in Java

When an if statement is defined within another statement, it is known as nested if statement. You can use any number of nested if and if-else construct.

Example

Belowe example will determine whether a year is a leap year or not.

public class Example
{
	public static void main(String args[])
	{
		int year = 2004;
		if(year%4 == 0)
		{
			if(year%100 == 0)
			{
				if(year%400 == 0)
				{
					System.out.println("It is a leap year");	
				}
				else
				{
					System.out.println("Not a leap year");
				}
			}
			else
			{
				System.out.println("It is a leap year");
			}
		}
		else
		{
			System.out.println("Not a leap year");
		}
	}
}

Output

It is a leap year