Home » Core Java » Nested If Statements in Java

About Mohammad Meraj Zia

Senior Java Developer

Nested If Statements in Java

In this article we will look how to write nested if Statements in Java.

1. Introduction in Java

Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3 billion devices run Java. It is used for mobile applications (especially Android apps), desktop applications, web servers, and application servers, games, and much more.

nested if statement

Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming language in the world. It is easy to learn and simple to use and is open-source and free. It is secure, fast and powerful and has a huge community support (tens of millions of developers). Java is an object-oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs.

2. Conditional Statements

Java, like all other programming languages, is equipped with specific statements that allow us to check a condition and execute certain parts of code depending on whether the condition is true or false. Such statements are called conditional and are a form of a composite statement. In Java, there are two forms of conditional statements:

  • the if-else statement, to choose between two alternatives;
  • the switch statement, to choose between multiple alternatives.

In this article we will learn how to use if-else conditional statement in java

2.1 Syntax

if (condition)

condition is an expression of type boolean, i.e., a conditional expression that is evaluated to true or false
then-statement is a single statement (also called the then-branch of the if-else statement)
else-statement is a single statement (also called the else-branch of the if-else statement)

First, the condition is evaluated. If the result of the evaluation is the value true, the then-statement is executed, otherwise, the else-statement is executed. In both cases, the execution continues with the statement immediately following the if-else statement.

3. Nested If Statements – Code

In this section we will see examples of nested if statements.


private static void printRange(int value) {
        if (value < 10) {
            System.out.println("Value should be greater that 9");
        if (value > 30) {
            System.out.println("Value should be less than 30");
        if (value >= 15) {
            if (value <= 25) {
                System.out.println("Value is between 15 and 25");

4. Summary

In this article we saw how to write a nested if statement in Java. We can write nested else-if statements as well but this article does not cover that.

5. Download The Source Code

This was an example of nested if statements in Java.

You can download the full source code of this example here: Nested if Statement in Java

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Daniel Escasa
2 years ago

The nested if, since it involves the same variable, can be rewritten as an and condition:

if ((value >= 15) && (value <= 25)) {

I suggest using a boolean expression involving a different variable