English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O/O)

Java Reader/Writer

Other Java topics

Java if...else Statements

In this tutorial, you will learn about control flow statements in Java with the help of examples, using Java's if and if ... else statements.

In computer programming, it is usually desired to execute a specific part of the code based on whether a specified condition is true or false (only known at runtime). In this case, control flow statements will be used.

Java if(if-then) statement

In Java,if-thenThe syntax of the statement is:

if (expression) {
    // statement
}

This is a boolean expression (expression). The boolean expression returns true or false.

  • If the expression is calculated as true, the statements within the if body (the statements within the parentheses) will be executed

  • If the calculation result of the expression is false, the statements within the if body will be skipped

How does the if statement work?

How else statements work

Example1:Java if statement

class IfStatement {
    public static void main(String[] args) {
        int number = 10;
        //Check if the number is greater than 0
        if (number > 0) {
            System.out.println("This number is positive.");
        }
        System.out.println("This statement is always executed.");
    }
}

Output:

This number is positive.
This statement is always executed.

In the above example, we have a variable named number. Here, the test expression checks if number is greater than 0 (number > 0).

Since number is greater than 0. Therefore, the calculation result of the test expression is true. At the same time, the code within the if will be executed.

Now, change the value of number to a negative integer. Assume it is -5.

int number = -5;

If we run the above program with the new value number, the output will be as follows:

This statement is always executed.

Here, the value of number is less than 0. Therefore, the calculation result of the test expression number > 0 is false. Therefore, the statements within the if will not be executed.

For more information about test expressions, please visitJava relational operators  andJava logical operators.

Java if ... else (if-then-else) statement

If the calculation result of the test expression of the if statement is true, the specific part of the code executed by the statement is executed. However, if the calculation result of the test expression is false, it will not perform any operation.

In this case, we can use the optional else code block. If the calculation result of the test expression is false, the else statement within the block is executed. This is calledif-then-elsestatement.

if-then-elseThe syntax of the statement is:

if (expression) {
   // code
}
else {
  // Other code
}

Here, if the test expression is true, our program will execute a task (the task within the if block), and if the test expression is false, another task (the task within the else code block).

How does the Java if... else statement work?

Java if-How else statements work

Example2:Java if else statement

class IfElse {
    public static void main(String[] args) {    	
        int number = 10;
        //Check if the number is greater than 0
        if (number > 0) {
            System.out.println("The number is positive.");
        }
        else {
            System.out.println("The number is not positive.");
        }
 
        System.out.println("This statement is always executed.");
    }
}

Output:

The number is positive.
This statement is always executed.

In the above example, we have a variable named number. Here, the test expression checks if number is greater than 0 (number > 0).

Since the value of number is10, so the result of the test expression calculation is true. Therefore, the code inside the if block is executed.

Now, change the value of number to a negative integer. For example -5.

int number = -5;

If we run the program with the new value number, the output will be:

The number is not positive.
This statement is always executed.

Here, the value of number is-5. Therefore, the result of the test expression calculation is false. So the code inside the else block is executed.

Java if..else..if statement

In Java, we have aif ... else ... if ladder statements, which can be used to execute one code block among multiple other code blocks.

if (expression1) {
   // code
}
else if (expression2) {
   // code
}
else if (expression3) {
   // code
}
.
.
else {
   // code
}

Here, the if statements are executed from top to bottom. Once a test expression is true, the code inside the if statement is executed. Then, the program control jumps to the if-else-Outside the if- ladder statement.

If all test expressions are false, execute the code inside the else block.

Example3Java if..else..if statement

class Ladder {
    public static void main(String[] args) {   
        int number = 0;
        //Check if the number is greater than 0 
        if (number > 0) {
            System.out.println("This number is positive.");
        }
        //Check if the number is less than 0
        else if (number < 0) {
            System.out.println("The number is negative.");
        }
        else {
            System.out.println("The number is 0.");
        } 
    }
}

Output:

The number is 0.

In the above example, we are checking whether the variable number is positive, negative, or zero. Here, we have two test expressions:

  • number > 0 - Check if number is greater than 0

  • number < 0 - Check if number is less than 0

Here, the value of number is 0. Therefore, the results of both test expressions are false. Hence, the statements inside the else block are executed.

Java nested if..else statement

In Java, you can also have if..else statements nested within if..else statements. This is called a nested if...else statement.

This is a search for3a program to find the largest number among a set of numbers:

Example4Nested if ... else statement

class Number {
    public static void main(String[] args) {
        //declare a double precision type variable
        Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largestNumber
        //Check n1whether it is greater than or equal to n2
        if (n1 >= n2) {
            //if ... else statement within the if block
            // Check n1whether it is greater than or equal to n3
            if (n1 >= n3) {
                largestNumber = n1;
            }
            else {
                largestNumber = n3;
            }
        }
            //if ... else statement in the else block
            //Check n2whether it is greater than or equal to n3
            if (n2 >= n3) {
                largestNumber = n2;
            }
                largestNumber = n3;
            }
        }
        System.out.println("The largest number is " + largestNumber);
    }
}

Output:

The largest number is 4.5

Note:In the above program, we ourselves assigned the values of the variables to simplify the demonstration process. However, in practical applications, these values may come from user input data, log files, form submissions, etc.

Java provides a feature calledTernary Operatora special operation ofoperator, it isif ... elseA shorthand form of a statement. To learn about the ternary operator, please visitJava Ternary Operator.