如果其他语句(C++)

控件条件分支。

if ( expression )
   statement1
[else
   statement2]

备注

如果 表达式的 值不为零, statement1 执行。 如果选项存在, statement2 执行,如果 表达式的 值为零。 表达式 必须是算术或指针类型,或者必须定义为算术或指针类型的一个明确的转换是类类型。 有关转换的信息, (,请参见 标准转换。)

如果 语句的两种形式, 表达式,它具有除结构的任何值,计算,包括任何副作用。 ,除非其中一个 语句包含 中断继续导航,控件从 如果 语句传递到过程的下一条语句。

if...else 语句的子句与在没有相应的语句的同一范围的最接近的前面 如果 语句。

为了使此示例可以明确的有关对的 if...else ,取消对大括号。

示例

// if_else_statement.cpp
#include <stdio.h>

int main() 
{
   int x = 0;
   if (x == 0)
   {
      printf_s("x is 0!\n");
   }
   else
   {
      printf_s("x is not 0!\n"); // this statement will not be executed
   }
   
   x = 1;
   if (x == 0)
   {
      printf_s("x is 0!\n"); // this statement will not be executed
   }
   else
   {
      printf_s("x is not 0!\n");
   }

   return 0;
}
      

请参见

参考

select语句(C++)

C++关键字

switch语句(C++)