Groovy - If/Else 语句
我们将看到的下一个决策语句是 if/else 语句。 这个语句的一般形式是 −
if(condition) {
statement #1
statement #2
...
} else{
statement #3
statement #4
}
该语句的一般工作是首先在 if 语句中评估条件。 如果条件为真,则执行其后的语句并在 else 条件之前停止并退出循环。 如果条件为假,则执行 else 语句块中的语句,然后退出循环。 下图显示了 if 语句的流程。
Following is an example of a if/else statement −
class Example {
static void main(String[] args) {
// Initializing a local variable
int a = 2
//Check for the boolean condition
if (a<100) {
//If the condition is true print the following statement
println("The value is less than 100");
} else {
//If the condition is false print the following statement
println("The value is greater than 100");
}
}
}
在上面的示例中,我们首先将变量初始化为值 2。然后我们评估变量的值,然后决定应该执行哪个 println 语句。 上述代码的输出将是
The value is less than 100.

