1
0

## If Else Statement in Java

This statement is used to alter the flow of control and is also known as an alteration or branching statement. The if-else statement is used in different forms as described below.

The simple if-statement

The if-statement is the most basic of all control flow statements. It tells your program to execute a certain section of code only if a particular condition evaluates to true. The syntax of if-statement is:

if ( <Expression> )
{
Body of if
}

In the above code, the statements inside the if-braces will be executed only if the condition is true. For example, consider the following code segment:

void isEven(int n)
{
if (n%2 == 0)
{
System.out.println("Number is even");
}
}

If the condition (n%2 == 0) evaluates to false (meaning that the number is odd), the control jumps to the end of the if-statement.

In addition, the opening and closing braces are optional, provided that the ‘if’ block contains only one statement:

void isEven(int n)
{
if (n%2 == 0)
System.out.println("Number is even"); // same as above code
}

Deciding when to omit the braces is a matter of personal taste. Omitting them can make the code more brittle. If a second statement is later added to the ‘if’ block, a common mistake would be forgetting to add the newly required braces. The compiler cannot catch this sort of error; you’ll just get the wrong results.

The if-else statement

The if-else statement provides a secondary path of execution when an “if” expression evaluates to false. The syntax of the if-else statement is:

if( <expression> )
{
Body of if
}
else
{
Body of else
}

In the above code, the statements inside the if-block will be executed only when the condition is true and the statement inside the else-part will be executed when the condition is false. You could use an if-else statement in the above code if you want to take some action if the number is odd. In this case, the action is to simply print a message stating that the number is odd.

void isEven(int n)
{
if(n%2 == 0)
{
System.out.println("Number is even"); // get printed if condition is true
}
else{
System.out.println("Number is odd"); // get printed if condition is false
}
}

The nested-if-else statement

The nested-if statement provides an execution path when the condition or expression evaluates to true and, it again checks for the condition inside the if-block or the else block, or in both blocks. The syntax for nested-if statement is:

if( <Expression1> )
{
Body of if; // executed if Expression1 is true
if( <Expression2> )
{
Body of nested if; //executed if Expression1 and Expression2 both are true
}
else{
Body of nested else; //executed if Expression1 is true and Expression2 is false
}
}
else{
Body of else; // executed if Expression1 is false
if( <Expression3> )
{
Body of nested if; // executed if Expression1 is false and Expression3 is true
}
else{
Body of nested else; // executed if Expression1 and Expression3 both are false
}
}

The following program illustrates the working of nested if-else statements with the help of an example.

Program to find the largest number out of three numbers.

class LargestNumber
{
public static void main(String args[])
{
int a = 5, b = 10, c = 7;
if(a > b)
{
if(a > c)
System.out.println("A is largest number");
else
System.out.println("C is largest number");
}
else
{
if(b > c)
System.out.println("B is largest number");
else
System.out.println("C is largest number");
}
}
}

Output:

B is largest number

This statement is also known as the if-else-if ladder. The if-else-if construct provides a secondary path of execution when the primary condition evaluates to false and another condition is checked inside the else-part. The syntax for the if-else-if statement is:

if ( <Expression1> )
{
Body of if;
}
else
{
Body of else;
if ( <Expression2> )
{
Body of else-if
}
else
....
}

The following example makes the use of if else if construct more clear.

Question: Write a program to assign grades based on the value of a test score: an A for a test score of 90% or above, a B for a test score of above 80% but below 90%, a C for a test score of above 70% but below 80%, D for a test score of above 60% but below 70% and F otherwise.

class TestGrade
{
public static void main(String[] args)
{
int testscore = 76;
if(testscore >= 90)
{
}
else if(testscore >= 80)
{
}
else if(testscore >= 70)
{
}
else if(testscore >= 60)
{
}
else
{
}
Grade = C