Java 简明教程

Java - if-else Statement

Java if-else Statement

Java 中, if else statement 用于根据给定的条件执行两个代码块。当 if 语句的布尔表达式为 true 时, Java if statement 执行。if 语句后可跟一个可选的 else 语句,当布尔表达式为 false 时,该语句执行。

In Java, the if else statement is used to execute two code blocks based on the given condition. A Java if statement executes when the Boolean expression for the if statement is true. An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Syntax of if-else Statement in Java

以下是 if…else 语句的语法:

Following is the syntax of an if…​else statement −

if(Boolean_expression) {
   // Executes when the Boolean expression is true
}else {
   // Executes when the Boolean expression is false
}

如果布尔表达式求值为 true,则将执行 if 代码块,否则将执行 else 代码块。

If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

Flow Diagram of if-else Statement in Java

if else statement

Example: Java if else statement

在此示例中,我们展示了 if else 语句的用法。我们创建了一个 variable x 并将其初始化为 30。然后在 if 语句中,我们使用 20 检查 x。由于 if 语句是 false,因此执行 else 块中的语句。

In this example, we’re showing the usage of if else statement. We’ve created a variable x and initialized it to 30. Then in the if statement, we’re checking x with 20. As if statement is false, the statement within the else block is executed.

public class Test {

   public static void main(String args[]) {
      int x = 30;

      if( x < 20 ) {
         System.out.print("This is if statement");
      }else {
         System.out.print("This is else statement");
      }
   }
}
This is else statement

Java if-else-if Statement

if…​else if…​else statement 用于基于给定条件(布尔表达式)执行多个代码块。

The if…​else if…​else statement is used for executing multiple code blocks based on the given conditions (Boolean expressions).

if 语句后面可以跟一个可选的 else if…​else 语句,这对于使用单个 if…​else if 语句测试各种条件非常有用。

An if statement can be followed by an optional else if…​else statement, which is very useful to test various conditions using a single if…​else if statement.

Points to Remember

使用 if-else if-else 语句时,需记住以下几点。

When using if-else if- else statements there are a few points to keep in mind.

  1. An if can have zero or one else’s and it must come after any else if’s.

  2. An if can have zero to many else if’s and they must come before the else.

  3. Once an else if succeeds, none of the remaining else if’s or else’s will be tested.

Syntax of if-else-if statement

以下是 if…​else if…​else 语句的语法 -

Following is the syntax of an if…​else if…​else statement −

if(Boolean_expression 1) {
   // Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2) {
   // Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3) {
   // Executes when the Boolean expression 3 is true
}else {
   // Executes when the none of the above condition is true.
}

Example 1: Java if … else if … else statement

在此示例中,我们展示了 if…else if…else 语句的使用。我们创建了一个变量 x 并将其初始化为 30。然后在 if 语句中,我们用 10 检查 x。由于 if 语句是 false,控制权跳转到 else if 语句,用另一个值检查 x,依此类推。

In this example, we’re showing the usage of if…​else if…​else statement. We’ve created a variable x and initialized it to 30. Then in the if statement, we’re checking x with 10. As if statement is false, control jumps to else if statement checking another value with x and so on.

public class Test {

   public static void main(String args[]) {
      int x = 30;

      if( x == 10 ) {
         System.out.print("Value of X is 10");
      }else if( x == 20 ) {
         System.out.print("Value of X is 20");
      }else if( x == 30 ) {
         System.out.print("Value of X is 30");
      }else {
         System.out.print("This is else statement");
      }
   }
}
Value of X is 30

Example 2: Java if … else if … else statement

在此示例中,我们展示了 if…else if…else 语句的使用。我们创建了一个变量 x 并将其初始化为 30.0。然后在 if 语句中,我们用 10,0 检查 x。由于 if 语句是 false,控制权跳转到 else if 语句,用另一个值检查 x,依此类推。

In this example, we’re showing the usage of if…​else if…​else statement. We’ve created a variable x and initialized it to 30.0. Then in the if statement, we’re checking x with 10,0. As if statement is false, control jumps to else if statement checking another value with x and so on.

public class Test {

   public static void main(String args[]) {
      double x = 30.0;

      if( x == 10.0 ) {
         System.out.print("Value of X is 10.0");
      }else if( x == 20.0 ) {
         System.out.print("Value of X is 20.0");
      }else if( x == 30.0 ) {
         System.out.print("Value of X is 30.0");
      }else {
         System.out.print("This is else statement");
      }
   }
}
Value of X is 30.0

Java Nested if-else Statement

当给定条件为 true 时, nested if else statement 用于在需要检查其他条件时更好地进行决策。在嵌套 if else 语句中,你可以让一个 if else 语句块阻止另一个 if(或 else)块。

The nested if else statement is used for better decision-making when other conditions are to be checked when a given condition is true. In the nested if else statement, you can have an if-else statement block the another if (or, else) block.

Syntax of nested if-else statement

以下是嵌套 if else 语句的语法:

Below is the syntax of nested if else statement:

if(condition1){
	// code block
	if(condition2){
		//code block
	}
}

Example: Java Nested if else Statement

以下示例使用嵌套 if..else 语句,在三个数中找到最大的数。

The following examples finds the largest number among three using nested if..else statement.

public class Test {

    public static void main(String[] args) {

        int x = 10, y = 20, z = 30;

        if(x >= y) {
            if(x >= z)
                System.out.println(x + " is the largest.");
            else
                System.out.println(z + " is the largest.");
        } else {
            if(y >= z)
                System.out.println(y + " is the largest.");
            else
                System.out.println(z + " is the largest.");
        }
    }
}
30 is the largest.