Core Java

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)
   then-statement
else
   else-statement

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.

NestedIfExample.java

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.

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

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Daniel Escasa
4 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

Back to top button