Sitemap

if-else Statement in Java

5 min readMay 27, 2025

--

if-else Statement:

In Java, the if-else statement is used for conditional branching, allowing you to execute different blocks of code based on whether a given condition is true or false.

Here is the basic syntax:

if (condition) {

// Code to be executed if the condition is true

} else {

// Code to be executed if the condition is false

}

Example-1:

public class IfElseExample {

public static void main(String[] args) {

int number = 10;

// Check if the number is greater than 5

if (number > 5) {

System.out.println(“The number is greater than 5.”);

} else {

System.out.println(“The number is not greater than 5.”);

}

}

}

In this example, if the number is greater than 5, the first block of code inside the if statement will be executed. Otherwise, the block of code inside the else statement will be executed.

You can also have multiple conditions using else if:

if (condition1) {

// Code to be executed if condition1 is true

} else if (condition2) {

// Code to be executed if condition2 is true

} else {

// Code to be executed if none of the above conditions are true

}

Example-2:

public class ElseIfExample {

public static void main(String[] args) {

int number = 0;

if (number > 0) {

System.out.println(“The number is positive.”);

} else if (number < 0) {

System.out.println(“The number is negative.”);

} else {

System.out.println(“The number is zero.”);

}

}

}

In this example, the program checks whether the number is positive, negative, or zero and prints the corresponding message based on the conditions.

if Statement:

In Java, the if statement is used for conditional branching. It allows you to execute a block of code only if a specified condition is true.

Here’s the basic syntax of the if statement:

if (condition) {

// Code to be executed if the condition is true

}

Example:

public class IfExample {

public static void main(String[] args) {

int number = 7;

// Check if the number is greater than 5

if (number > 5) {

System.out.println(“The number is greater than 5.”);

}

// This line will be executed regardless of the condition

System.out.println(“This line is always executed.”);

}

}

In this example, if the number is greater than 5, the code inside the if block will be executed, and the corresponding message will be printed. If the condition is false, the code inside the if block will be skipped.

You can also use curly braces {} to define a block of code to be executed if the condition is true. While braces are optional when you have only one statement inside the if block, it’s considered good practice to use them for clarity and to avoid potential issues with indentation.

Remember that the condition in the if statement must be a boolean expression, meaning it evaluates to either true or false. If the condition is true, the code inside the if block is executed; otherwise, it is skipped.

Here’s another example:

public class IfElseExample {

public static void main(String[] args) {

int x = 10;

if (x % 2 == 0) {

System.out.println(“The number is even.”);

} else {

System.out.println(“The number is odd.”);

}

}

}

In this example, the program checks whether the value of x is even or odd and prints the corresponding message.

if-else-if ladder in Java:

In Java, an if-else-if ladder is a series of if and else if statements that allow you to test multiple conditions in sequence. Each else if statement is only evaluated if the preceding if or else if conditions are false.

Here’s the basic 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

Get Manisha Shukla’s stories in your inbox

Join Medium for free to get updates from this writer.

} else {

// Code to be executed if none of the above conditions are true

}

Example:

public class IfElseIfExample {

public static void main(String[] args) {

int number = 0;

if (number > 0) {

System.out.println(“The number is positive.”);

} else if (number < 0) {

System.out.println(“The number is negative.”);

} else {

System.out.println(“The number is zero.”);

}

}

}

In this example, the program checks whether the number is positive, negative, or zero and prints the corresponding message. Only one block of code will be executed based on the first true condition encountered. If none of the conditions is true, the code inside the else block will be executed.

You can have as many else if statements as needed, allowing you to test multiple conditions in a structured manner. It’s important to note that once a true condition is found, the corresponding block of code is executed, and the rest of the else if ladder is skipped.

Here’s another example with multiple else if statements:

public class GradeExample {

public static void main(String[] args) {

int score = 75;

if (score >= 90) {

System.out.println(“A”);

} else if (score >= 80) {

System.out.println(“B”);

} else if (score >= 70) {

System.out.println(“C”);

} else if (score >= 60) {

System.out.println(“D”);

} else {

System.out.println(“F”);

}

}

}

This example assigns a letter grade based on the value of the score variable. The first true condition encountered will determine the grade printed.

nested if statement in Java:

In Java, a nested if statement is an if statement that is placed inside another if or else block. This allows you to create more complex conditional structures by testing multiple conditions.

Here’s the basic syntax:

if (outerCondition) {

// Code to be executed if outerCondition is true

if (innerCondition) {

// Code to be executed if both outerCondition and innerCondition are true

}

}

Example:

public class NestedIfExample {

public static void main(String[] args) {

int x = 10;

int y = 5;

if (x > 0) {

System.out.println(“x is positive.”);

if (y > 0) {

System.out.println(“y is also positive.”);

} else {

System.out.println(“y is not positive.”);

}

} else {

System.out.println(“x is not positive.”);

}

}

}

In this example, the program first checks if x is positive. If x is positive, it enters the nested if statement and checks whether y is positive. Depending on the conditions, different messages will be printed.

It’s important to maintain proper indentation and braces to clearly indicate the nested structure. Also, keep in mind that excessive nesting can make your code harder to read, so it’s often a good idea to refactor your code or use other control flow structures like else if ladders or switch statements when possible.

Here’s an example with multiple levels of nesting:

public class NestedIfExample2 {

public static void main(String[] args) {

int a = 5;

int b = 10;

int c = 15;

if (a > 0) {

System.out.println(“a is positive.”);

if (b > 0) {

System.out.println(“b is positive.”);

if (c > 0) {

System.out.println(“c is positive.”);

} else {

System.out.println(“c is not positive.”);

}

} else {

System.out.println(“b is not positive.”);

}

} else {

System.out.println(“a is not positive.”);

}

}

}

--

--

Manisha Shukla
Manisha Shukla

Written by Manisha Shukla

I write on software testing( Manual testing ,Automation,API, Java etc.)