×

Groovy 教程

Groovy 概述Groovy 环境Groovy 基本语法Groovy 数据类型Groovy 变量Groovy 运算符Groovy 循环Groovy 条件语句Groovy 方法Groovy 文件I/OGroovy 可选Groovy 数字Groovy 字符串Groovy 范围Groovy 列表Groovy 映射Groovy 日期和时间Groovy 正则表达式Groovy 异常处理Groovy 面向对象Groovy 泛型Groovy 特征Groovy 闭包Groovy 注释Groovy XMLGroovy JMXGroovy JSONGroovy DSLSGroovy 数据库Groovy 构建器Groovy 命令行Groovy 单元测试Groovy 模板引擎Groovy 元对象编程

Groovy If/Else语句


Groovy 条件语句Groovy 条件语句


我们将看到的下一个决策语句是 if / else 语句。这种说法的一般形式是 -

if(condition) { 
   statement #1 
   statement #2 
   ... 
} else{ 
   statement #3 
   statement #4  
}

这个语句的一般工作是首先在 if 语句中评估一个条件。如果条件为真,则其后执行语句,并在else条件之前停止并退出循环。如果条件为假,则执行else语句块中的语句,然后退出循环。下图显示了 if 语句的流程。

If Statements

下面是一个if / else语句的例子 -

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.

Groovy 条件语句Groovy 条件语句


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)