有時(shí),需要將多個(gè) if 語句嵌入到彼此內(nèi)部,這在其他編程語言中是可能的。在 Erlang,這也是可能的。
下圖是嵌套if語句的圖形表示。
下面的程序中顯示了一個(gè)示例:
-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.在上述程序中,應(yīng)注意以下幾點(diǎn)-
當(dāng)?shù)谝粋€(gè)if條件的值為時(shí)true,則開始第二個(gè)if條件的評估。
上面的代碼的輸出將是-
A is less than 5