if else 语句

在 Java 中,if else 语句用于执行基于以下条件的两个代码块:给定的条件。当 if 语句的布尔表达式为 true 时,Java if 语句就会执行。 if 语句后面可以跟一个可选的 else 语句,该语句在布尔表达式为 false 时执行。

语法

以下是 if 的语法...else 语句 -

if(Boolean_expression) {
   // 当布尔表达式为 true 时执行
}else {
   //布尔表达式为 false 时执行
} 

如果布尔表达式的计算结果为 true,则将执行 if 代码块,否则将执行 else 代码块。

流程图

Java - if-else 语句

示例:Java if else 语句

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

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 

if...else if...else 语句

if...else if...else 语句用于根据给定条件(布尔表达式)执行多个代码块。

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

要记住的要点

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

  • 一个 if 可以有零个或一个 else if,并且它必须位于任何 else if 之后。

  • 一个 if 可以有零到多个 else if,并且它们必须位于 else if 之前else。

  • 一旦 else if 成功,则不会测试其余的 else if 或 else。

语法

以下是 if...else if...else 语句的语法 -

if(Boolean_expression 1) {
   //布尔表达式1为true时执行
}else if(Boolean_expression 2) {
   //布尔表达式2为true时执行
}else if(Boolean_expression 3) {
   //布尔表达式3为true时执行
}else {
   // 当上述条件都不成立时执行。
} 

示例 1:Java if ... else if ... else 语句

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

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 

示例 2:Java if ... else if ... else 语句

在此示例中,我们将展示 if...else if 的用法...其他声明。我们创建了一个变量 x 并将其初始化为 30.0。然后在 if 语句中,我们用 10,0 检查 x。当 if 语句为 false 时,控制跳转到 else if 语句,用 x 检查另一个值,依此类推。

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 

嵌套的 if else 语句

当给定条件为真时要检查其他条件时,嵌套的 if else 语句用于更好地做出决策。在嵌套的 if else 语句中,您可以让一个 if-else 语句块另一个 if (or, else) 块。

语法

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

if(condition1){    
	//代码块
	if(condition2){  
		//代码块
	}    
} 

示例:Java 嵌套 if else 语句

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

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.