Java 嵌套if语句

来源:这里教程网 时间:2026-02-17 20:16:15 作者:

Java 嵌套if语句

嵌套if-else语句始终是合法的,这意味着您可以在另一个if或else if语句内部使用一个if或else if语句。

语法

嵌套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语句一样,将else if...else嵌套在 ****标签中。

示例

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");         }      }   }}

这将产生以下结果−

输出

X = 30 and Y = 10

相关推荐