在Lua编程内嵌if-else语句,这意味着可以使用一个 if 或 else if 在另一个语句if或else if 语句中。
语法
if语句的嵌套语法如下:
if( boolean_expression 1) then --[ Executes when the boolean expression 1 is true --] if(boolean_expression 2) then --[ Executes when the boolean expression 2 is true --] end end
您可以嵌套else if...else 以类似if语句的方式。
例子:
--[ local variable definition --] a = 100; b = 200;--[ check the boolean condition --] if( a == 100 ) then --[ if condition is true then check the following --] if( b == 200 ) then --[ if condition is true then print the following --] print("Value of a is 100 and b is 200" ); end end print("Exact value of a is :", a ); print("Exact value of b is :", b );
当建立和运行上面的代码,它会产生以下结果。
Value of a is 100 and b is 200 Exact value of a is : 100 Exact value of b is : 200
前面章节中,详细介绍了 3 种形式的条件语句,即 if、if else 和 if elif else,这 3 种条件语句之间可以相互嵌套。 例如,在最简单的 if 语句中嵌套 if else 语句,形式如下: if 表达式 1: if 表示式 2: 代码块 1 else: 代码块 2 再比如,在 if else 语句中嵌套 if else 语句,形式
Swift 条件语句 在 Swift 语言中,你可以在一个 if 或 else if 语句内使用另一个 if 或 else if 语句。 语法 Swift 语言中 嵌套 if 语句的语法: if boolean_expression_1 { /* 当 boolean_expression_1 表达式 true 时执行 */ if boolean_expression_2 {
C++ 判断 在 C++ 中,嵌套 if-else 语句是合法的,这意味着您可以在一个 if 或 else if 语句内使用另一个 if 或 else if 语句。 语法 C++ 中 嵌套 if 语句的语法: if( boolean_expression 1) { // 当布尔表达式 1 为真时执行 if(boolean_expression 2) { // 当布尔
在VB.Net中嵌套If-Then-Else语句总是合法的,这意味着你可以在另一个If ElseIf语句中使用一个If或ElseIf语句。 语法 (Syntax) 嵌套If语句的语法如下 - If( boolean_expression 1)Then 'Executes when the boolean expression 1 is true If(boolean_expressi
在Objective-C编程中nest if-else语句总是合法的,这意味着你可以在另一个if或else if语句中使用if或else if语句。 语法 (Syntax) nested if语句的语法如下 - if( boolean_expression 1) { /* Executes when the boolean expression 1 is true */ if(bool
在Pascal编程中嵌套if-else语句总是合法的,这意味着你可以在另一个if或else if语句中使用if或else if语句。 Pascal允许嵌套到任何级别,但是,如果依赖于特定系统上的Pascal实现。 语法 (Syntax) 嵌套if语句的语法如下 - if( boolean_expression 1) then if(boolean_expression 2)then S1 e