In programming languages like Java, there are certain control statements that are utilized to manage the flow of code. Based on these statements, the code either advances to further execution or stops at a particular statement. One of the decision-making statements is the if statement. In this write-up, we will demonstrate how “if” statements are used in Java coding to perform specific tasks.
How to Use If Statements in Java?
The if statement is considered one of the simplest decision-making statements of Java. It checks whether certain statements will execute or not. It works in such a manner that if a certain condition appears to be true, the statements are executed otherwise the statements are not executed. Java supports the below-listed forms of the if statements:
- The Basic “if” Statement
- “if-else” statement
- “if-else-if” ladder statement
- “nested if” statement
Below is the implementation of different types of if statements in Java.
- The Basic “if” Statement
The if statement accepts a specific condition and executes the associated code block only when the specified condition remains true. The statements/code associated with the if-statement will be skipped if the condition is not true. Consider the following code block for a profound understanding of the basic if statement in Java.
public class Example1 {
public static void main(String[] args) {
System.out.print("Implementation of if statement");
int num=30;
//If the number is greater than the specified number print the result accordingly
if(num>17){
System.out.print("The number is greater than 17");
}
}
}
In the above Java code:
- The first step involves declaring a class as “Example1”.
- An integer value is declared as “num”.
- The next step involves the if statement to check whether the specified value is greater than the entered value or not.
- The println() method prints the result if the value number is greater.
Output
The output below shows that the number 30 is greater than the number 17 therefore the result is printed as “The number is greater than 17”.
- if-else Statement
The if-else block deals with both “true” and “false” conditions. It is such that the if block gets executed when the provided condition is “true”, otherwise the else block gets executed/operated. Consider the following code block for a profound understanding of the basic if-else statement in Java.
//Class to implement the if-else statement in Java
public class IfElsestatement {
public static void main(String[] args) {
//Declare an integer value to be checked
int number=14;
//If the number is not divisible by 2.
if(number%2!=0){
System.out.println("The declared number is an odd number");
}
//If the number is divisible by 2
else{
System.out.println("The declared number is an even number");
}
}
}
In the above Java code:
- The class for the if-else statement is declared.
- An integer value is declared as a “number”.
- The next step involves the if statement which contains the condition that the specified number is not divisible by 2.
- If the provided number divided by 2 is not equal to zero, then it is declared as odd, otherwise, even.
Output
The below output depicts that 14 is an even number since it is divisible by 2 therefore else statement is executed.
- If-else-if Ladder Statement
The if-else-if statement is employed to execute one statement from several statements.
class ladderstatement {
public static void main(String[] args) {
//number to be tested
int num = -15;
if (num > 0) {
System.out.println("The entered number is positive.");
}
//Condition to check for a number that is less than 0
else if (num < 0) {
System.out.println("The entered number is negative.");
}
//If the above conditions are false
else {
System.out.println("The entered number is 0.");
}
}
}
In the above code block:
- The Main class of Java consists of an integer value declared as “num”.
- The if statement has a condition that states the given value must be greater than zero.
- The else-if consists of the condition that prints the result using the println() method when the number is less than 0.
- The else part gets executed when the provided value is zero.
Output
The below output depicts that the entered number “-15” is less than 0 and therefore is a negative number.
- Nested if-else Statement
The term “nested if-else” refers to the if-else statement declared within another if-else. The code below implements the nested if-statement in Java.
public class JavaNestedstatement {
public static void main(String[] args) {
int Maths=80;
int Programming=80;
//Apply the conditions on Maths and Programming respectively.
if(Maths>=80){
if(Programming>=80){
//Print statement if the exam is passed.
System.out.println("You have passed the Exam");
}else{
//Print statement for the execute of else statement
System.out.println("You have not passed the Exam");
}
} else{
System.out.println("Reappear in the Exam ");
}
} }
In the above block of code:
- The class is declared as “JavaNestedstatement”.
- Two integer values are declared as “Maths” and “Programming”.
- The if statement tests the specified expression if it’s “>= 80”. If yes, then the program proceeds with the nested if statement.
- If the criteria (Maths>=80) specified in the nested if statement is met, the associated code block will get executed.
- The else statements print the result using the println() method when the if condition goes false.
Output
The below output depicts that the if condition is true therefore the result is printed as “You have passed the Exam”.

Conclusion
The if statement is one of the decision-making statements in Java that works in a manner that a certain block of code executes if the condition appears to be true. Otherwise, the code associated with the if statement will be skipped. In this write-up, we have demonstrated in detail the implementation of if statements along with their different types in Java.