Java if and else statements

The if and else statements are used in Java to control the flow of the program script. With these statements, we give our program the ability to decide what to do in a given situation.

The working of the if and else statement in Java is very straightforward:

  1. The syntax of an if statement consists of a condition written within parentheses '()' followed by its corresponding code block, enclosed in curly braces '{}'.

    			
    
    if (condition) {
    	block of code
    }
    
    				
  2. If the condition holds true, the java interpreter executes the set of instructions within the code block. If the condition evaluates to false, it will skip the control statement to the next statement.

    			
    
    if (condition) {
    	statement_1;
    }
    next-statement;
    
    				
  3. The else statement can be used after the if statement to execute another block of code in case the condition evaluates to false.

    			
    
    if (condition) {
    	statement_1;
    } else {
    	statement_2;
    }
    next-statement;
    
    				

In the above examples, we can summarize that 'statement_1' is executed if the condition evaluates to true, after which the control would flow to the 'next-statement'. If the condition evaluates to false, the control flows either to 'statement_2' in the presence of an else statement or to the 'next-statement', completely skipping the control statement in case an else statement is absent.

Code Snippet: Code to evaluate marks using if statement

			

String Grade = "Pass";
if (marks < 40) {
	Grade = "Fail";
}
System.out.println(Grade);

				

If we use an else statement:

			

String Grade = "";
if (marks < 40) {
	Grade = "Fail";
} else {
	Grade = "Pass";
}
System.out.println(Grade);

				

Braceless If-else statements

If there is only a single statement within an if or else code block, the use of braces is unnecessary.

The aforementioned code can be rewritten as:

			

String Grade = "Pass";
if (marks < 40) Grade = "Fail";
System.out.println(Grade);

				

And:

			

String Grade = "";
if (marks < 40) Grade = "Fail";
else Grade = "Pass";
System.out.println(Grade);

				

This kind of brace-less style is risky and prone to errors. It is recommended to always use braces.

else if

In Java, an if statement can be used after an else within an if-else statement to further extend its functionality and operate on one more condition.

			

String Grade = "";
if (condition_1) {
	statement_1;
} else if (condition_2) {
	statement_2;
} else {
	statement_3;
}
System.out.println(Grade);

				

In this example, the 'condition_1' is given more weight than 'condition_2'. The second condition is only evaluated if the first condition holds false.

Nested if statements

An if statement can be written within another if statement. This is called a nested if statement. Here, inner if statements are evaluated only if the outer if statements are executed. The syntax of a set-up nested if statements are:

			

if (expression_1) {
	if (expression_2) {
	…
		if (expression_n) {
	…
	}
}

				

Please note that a nested if statement can also be used within an else statement.

There is no theoretical limit on the number of levels of nested if statements. This enables the program to give specific instructions on specific conditions efficiently.

The given example demonstrates nested if statements:

			
	
String Grade = "";
if (marks > 0) {
    if (marks < 100) { 
    	if (marks > 40) {
        	Grade = "Pass";
    	} else {
    	    Grade = "Fail";
    	}
    } else {
        Grade = "N/A";
	    System.out.println("marks can not be more than 100\n");
    }
} else {
    Grade = "N/A";
}
System.out.println(Grade);

				

if-else ladder

An if-else ladder is used to evaluate multiple conditions, each with its specific statements. For example:

			

If (condition_1) {
	Statement_1;
} else if (condition_2) {
	Statement_2;
} … else {
	Statement;     //executed if all given conditions hold false
}

				
For Example:
			

String Grade = "";
if (marks > 100) {
    Grade = "N/A";
    System.out.println("marks can not be more than 100\n");
} else if (marks > 90) {
    Grade = "A";
} else if (marks > 70) {
	Grade = "B";
} else if (marks > 50) {
	Grade = "C";
} else if (marks > 30) {
	Grade = "D";
} else if (marks > 10) {
	Grade = "E";
} else if (marks > 0) {
	Grade = "f";
} else {
	Grade = "N/A";
} 
System.out.println(Grade);

				

The conditions are evaluated from top to bottom, one after the other, until one of the conditions evaluates to true. One thing to remember is that the if-else ladder does not execute the rest of the code if one of the conditions evaluates to true. It skips the remaining control statements and executes the code next to the ladder statement.