Swift if-else 语句

2024年8月29日 | 1 分钟阅读

Swift if-else 语句包含两个语句:if 语句和 else 语句。

如果测试评估为真,则执行 if 语句;如果测试评估为假,则执行 else 语句。

语法

示例

输出

This is a positive number.
This will be executed anyways.

在上面的程序中,常量 number 是正数 (5),因此,测试用例的评估结果为真,并执行 if 代码块内的语句。

让我们将常量 number 的值更改为负数 (-5),并且测试条件保持不变。

输出

This is not a positive number.
This will be executed anyways.

您可以看到它执行 else 代码块内的语句。