C++ if 关键字
示例
测试两个值,判断 20 是否大于 18。如果条件为 true
,则打印一些文本。
if (20 > 18) {
cout << "20 is greater than 18";
}
定义和用法
The if
statement specifies a block of code to be executed if a condition is true
。
C++ 拥有以下条件语句
- 使用
if
来指定一个代码块,当指定条件为 true 时执行。 - 使用
else
来指定一个代码块,如果相同条件为 false 则执行。 - 使用
else if
来指定一个新条件进行测试,如果第一个条件为 false。 - 使用
switch
来指定多个可供选择的代码块来执行。
更多示例
相关页面
Read more about conditions in our C++ Conditions Tutorial。