Latest web development tutorials

C ++ if statement

C ++ judgment C ++ judgment

If a Boolean expression followed by a statementby the one or more statements.


C ++if statement syntax:

   // 如果布尔表达式为真将执行的语句

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

The if statement in C ++


#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

C ++ judgment C ++ judgment