Java if else statement

The Java if else statement executes a block of code, if a specified condition holds true. If the condition is false, another block of code can be executed using the else statement.

In order to create the conditional statements used in a Java if else statement, you should know about all the different operators you can use.


Syntax

You can see a total of three code blocks in the Syntax example below. Only the first if block is actually compulsory. The else if and and else blocks are add-ons which increase the functionality of the if statement.

if (condition) {
     #Statement to be executed
}
else if (condition) {   
     #Statement to be executed
}
else {         
     #Statement to be executed
}

if statement:

Most of the time, the if statement is all you will need. It is used to check if a given condition, and execute a block of statement if it was true.

int a = 5;

if (a > 0) {
      System.out.println("a is greater than 0");
}
//Output 
a is greater than 0

Else:

The else statement triggers if all conditions in the if statement were false. (This includes elif statements). In other words, it’s a block that runs if all else has failed.

int b = 2;

if (a > b) {
    System.out.println("a greater than b") 
else:
    System.out.println("b is greater than a")
}

We haven’t specified a value for a here under the assumption that the user will be inputting a value.


Else if:

We aren’t restricted to just one condition. Using the else if statement we can define multiple conditions, thus multiple paths that the program flow might take.

Furthermore, unlike the else statement we can define multiple Else if statements. There is no limit save for readability issues on the number of Else if statements. In fact, if you want to create a large number of possible paths, you should use the select statement to maintain readability.

Keep in mind however that whatever happens, only one path can be taken during one execution. Only one out of the many code blocks will be executed. This applies to all the code blocks in a Python if else statements, including if and else code blocks.


Example

Here’s an example utilizing all three different statements.

public class example2 {
	public static void main(String[] args) {
		int a = 5;
		
		if (a > 0) {
			System.out.println("Number is positive");
		}
		else if (a < 0) {
			System.out.println("Number is negative");
		}
		else {
			System.out.println("Number is zero");
		}	
	}
}

Since neither of the first two conditions account for the number 0, we added the else statement.


Nested if statements

Often the use of a nested if statement in Java comes helps in dealing with complex scenarios. A nested if statement, is a if statement within an if statement. See the general syntax form below.

(There may be additional instructions between the two if statements)

if(condition) {
    if(condition) {
         // Instructions
    }
}

Extra Examples

In Java, there’s a way to write a shorter if else statement, comprising of just one line. Good for readability and conserving number of lines. Known as the “short hand” if else statement.

if (a > 0) System.out.println("a is greater than 0");

Note that there were no curly brackets required in this one line if statement.


This marks the end of the Java if else statement article. Any suggestions or contributions for CodersLegacy are more than welcome. You can ask any relevant questions in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments