Groovy 嵌套If語(yǔ)句

2018-12-29 14:23 更新

有時(shí)需要有多個(gè)if語(yǔ)句嵌入在彼此內(nèi)部。

這種說(shuō)法的一般形式是 -

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

以下是嵌套if / else語(yǔ)句的示例 -

class Example { 
   static void main(String[] args) { 
      // Initializing a local variable 
      int a = 12 
		
      //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 
         // Check if the value of a is greater than 5 
			
      if (a>5) { 
         //If the condition is true print the following statement 
         println("The value is greater than 5 and greater than 100"); 
      } else { 
         //If the condition is false print the following statement 
         println("The value of a is less than 5"); 
      }  
   } 
}	     

在上面的例子中,我們首先將一個(gè)變量初始化為值12.在第一個(gè) if 語(yǔ)句中,我們看到 a 的值是否大于100。如果沒(méi)有,那么我們進(jìn)入第二個(gè)for循環(huán),看看 a 的值是否大于5或小于5.上面的代碼的輸出將是 -

The value is greater than 5 and greater than 100
以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)