课程表

Groovy课程

工具箱
速查手册

Groovy 嵌套If语句

当前位置:免费教程 » Java相关 » Groovy

有时需要有多个if语句嵌入在彼此内部。

这种说法的一般形式是 -

  1. if(condition) {
  2. statement #1
  3. statement #2
  4. ...
  5. } else if(condition) {
  6. statement #3
  7. statement #4
  8. } else {
  9. statement #5
  10. statement #6
  11. }

以下是嵌套if / else语句的示例 -

  1. class Example {
  2. static void main(String[] args) {
  3. // Initializing a local variable
  4. int a = 12
  5. //Check for the boolean condition
  6. if (a>100) {
  7. //If the condition is true print the following statement
  8. println("The value is less than 100");
  9. } else
  10. // Check if the value of a is greater than 5
  11. if (a>5) {
  12. //If the condition is true print the following statement
  13. println("The value is greater than 5 and greater than 100");
  14. } else {
  15. //If the condition is false print the following statement
  16. println("The value of a is less than 5");
  17. }
  18. }
  19. }

在线运行实例

在上面的例子中,我们首先将一个变量初始化为值12.在第一个 if 语句中,我们看到 a 的值是否大于100。如果没有,那么我们进入第二个for循环,看看 a 的值是否大于5或小于5.上面的代码的输出将是 -

  1. The value is greater than 5 and greater than 100
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号