Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

if-else
java
conditional

How to use if statements in Java

Aaron Xie

Today, you will learn about conditional logic in Java and how to use if statements.

if statement

Below, is the syntax for an if-then statement in Java:

if (condition) {
    commands
}

The condition will either return true or false. If the condition is true, the commands within the if statement will execute. If the condition is false, the statements will not execute and the program will move on to the next lines of code.

Example

class main {
    public static void main( String args[] ) {
        int num = 15;

        if(num < 10){
            System.out.println("num is less than 10");
        }

        if(num > 10){
            System.out.println("num is greater than 10");
        }
    }
}

In the example above, we have an integer variable called num with a value of 1515. The first if statement checks if num is less than 1010. Since 1515 is not less than 1010, the command within the first if statement will not execute.

The next if statement checks whether num is greater than 1010. Because 1515 is greater than 1010, the print statement nested below will be executed.

ifelse statement

if (condition) {
    commands
} else {
    commands
}

Looking at the syntax above, the if statement will check a condition. If the condition is true, the commands nested under the if statement will be executed. Then, it will skip past the else statement.

However, if the condition in the if statement is false, the commands under the else statement will be executed.

Example

class main {
    public static void main( String args[] ) {
        int num = 15;

        if (num < 10) {
            System.out.println("num is less than 10");
        } else {
            System.out.println("num is greater than 10");
        }
    }
}

In the example above, we once again have an integer variable called num. The if statement checks whether num is less than 1010. Because num has a value of 1515, the print statement nested below the if statement will not execute.

Because the condition in the if statement returned false, the command under the else statement will run and print out, num is greater than 10.

If you are looking for a simplified way to write an ifelse statement, consider reading about ternary operators in Java.

ifelse ifelse statement

if (condition) {
    commands
} else if (condition) {
    commands
} else (condition) {
    commands
}

When working with ifelse ifelse statements, the first if statement will check a condition. If the condition is true, the commands nested below it will run and then skip past the else if and else statements.

However, if the if condition is false, the program will move onto the else if statement and evaluate its condition. If the condition is true, the commands nested below it will execute and the program will skip past the else statement. If the condition is false, the commands nested under the else statement will execute.

class main {
    public static void main( String args[] ) {
        int num = 20;

        if (num < 10) {
            System.out.println("num is less than 10");
        } else if (num > 10 && num <= 15) {
            System.out.println("num is greater than 10 and less than 15");
        } else {
            System.out.println("num is greater than 15");
        }
    }
}

It’s also possible to have multiple else if statements if you would like to handle more cases.

If you’re interested in learning about other conditional statements, check out our Edpresso shot about the switch statement in Java.

RELATED TAGS

if-else
java
conditional

CONTRIBUTOR

Aaron Xie
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring