Groovy - if 語句



第一個決策制定語句是 if 語句。此語句的常規形式如下 −

if(condition) { 
   statement #1 
   statement #2 
   ... 
}

此語句的一般工作原理是首先在if語句中求值一個條件。如果條件為真,則執行語句。下圖顯示了 if 語句的流程。

If Statement

接下來是 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"); 
      } 
   } 
}

在上例中,我們首先將變數初始化為值 2。然後,我們求值變數的值,然後決定是否執行 println 語句。上述程式碼的輸出如下 −

The value is less than 100
groovy_decision_making.htm
廣告
© . All rights reserved.