C ++ if statement
If a Boolean expression followed by a statementby the one or more statements.
grammar
C ++if statement syntax:
if(boolean_expression) { // 如果布尔表达式为真将执行的语句 }
If the Boolean expression istrue, the if statement within a block of code will be executed.If the Boolean expression isfalse, if the first set of codes after the statement (after the close parenthesis) will be executed.
C language to anynon-zero and non-nullvalue is assumed to betrue,thezeroornullassumed to befalse.
flow chart
Examples
#include <iostream> using namespace std; int main () { // 局部变量声明 int a = 10; // 使用 if 语句检查布尔条件 if( a < 20 ) { // 如果条件为真,则输出下面的语句 cout << "a 小于 20" << endl; } cout << "a 的值是 " << a << endl; return 0; }
When the above code is compiled and executed, it produces the following results:
a 小于 20 a 的值是 10