Groovy If語句

2018-12-29 14:19 更新

第一個決策語句是 if 語句。這種說法的一般形式是 -

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

這個語句的一般工作是首先在 if 語句中評估一個條件。如果條件為真,它然后執(zhí)行語句。下圖顯示了 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.然后我們評估變量的值,然后決定是否應(yīng)該執(zhí)行 println 語句。上面的代碼的輸出將是 -

The value is less than 100
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號