Erlang - 巢狀if語句



有時,需要在彼此內部巢狀多個if語句,這在其他程式語言中是可能的。在 Erlang 中,這同樣也是可能的。

下圖是巢狀if語句的圖表表示。

Nested if Statements

以下程式展示了一個示例:

示例

-module(helloworld). 
-export([start/0]). 

start() -> 
   A = 4, 
   B = 6, 
   if 
      A < B ->
         if 
            A > 5 -> 
               io:fwrite("A is greater than 5"); 
            true -> 
               io:fwrite("A is less than 5")
         end;
      true -> 
         io:fwrite("A is greater than B") 
   end.

在上述程式中,應注意以下幾點:

  • 當第一個if條件計算結果為true時,它將開始評估第二個if條件。

上述程式碼的輸出將是:

輸出

A is less than 5
erlang_decision_making.htm
廣告

© . All rights reserved.