有时,需要将多个 if 语句嵌入到彼此内部,这在其他编程语言中是可能的。在 Erlang,这也是可能的。
下图是嵌套if语句的图形表示。
下面的程序中显示了一个示例:
-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