当前位置: 首页 > 文档资料 > 学习 Java 编程 >

nested if statement

优质
小牛编辑
123浏览
2023-12-01

嵌套if-else语句总是合法的,这意味着你可以在另一个if或else if语句中使用if或else if语句。

语法 (Syntax)

嵌套if ... else的语法如下 -

if(Boolean_expression 1) {
   // Executes when the Boolean expression 1 is true
   if(Boolean_expression 2) {
      // Executes when the Boolean expression 2 is true
   }
}

您可以使用与嵌套if语句类似的方式嵌套if else if...else

例子 (Example)

public class Test {
   public static void main(String args[]) {
      int x = 30;
      int y = 10;
      if( x == 30 ) {
         if( y == 10 ) {
            System.out.print("X = 30 and Y = 10");
         }
      }
   }
}

这将产生以下结果 -

输出 (Output)

X = 30 and Y = 10