if statement
IF语句
有条件地执行另一条语句。
用于需要根据运行时或编译时条件执行代码的地方。
句法
attr(optional) if ( condition ) statement-true (1) attr(optional) if ( condition ) statement-true else statement-false (2) | attr(optional) if ( condition ) statement-true | (1) | | attr(optional) if ( condition ) statement-true else statement-false | (2) | | (until C++17) |
---|---|---|---|---|---|---|---|
attr(optional) if ( condition ) statement-true | (1) | | |||||
attr(optional) if ( condition ) statement-true else statement-false | (2) | | |||||
attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement-true (1) attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement-true else statement-false (2) | attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement-true | (1) | | attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement-true else statement-false | (2) | | (since C++17) |
attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement-true | (1) | | |||||
attr(optional) if constexpr(optional) ( init-statement(optional) condition ) statement-true else statement-false | (2) | |
attr(C++11) | - | any number of attributes |
---|---|---|
condition | - | one of expression which is contextually convertible to bool declaration of a single non-array variable with a brace-or-equals initializer. |
init-statement(C++17) | - | either an expression statement (which may be a null statement ";") a simple declaration, typically a declaration of a variable with initializer, but it may declare arbitrary many variables or be a decomposition declaration Note that any init-statement must end with a semicolon ;, which is why it is often described informally as an expression or a declaration followed by a semicolon. |
statement-true | - | any statement (often a compound statement), which is executed if condition evaluates to true |
statement-false | - | any statement (often a compound statement), which is executed if condition evaluates to false |
- 表达那就是上下文可转换下流
- 声明具有大括号或相等值的单个非数组变量的初始化器...
init-statement(C++17) - either
- 安表达式语句%28,这可能是
空语句
“;
“%29
- 阿简单声明,通常是带有初始化器的变量的声明,但它可以声明任意多个变量,也可以声明为分解声明。
注意,任何init语句都必须以分号结尾。;
,这就是为什么它经常被非正式地描述为一个表达式或一个声明,后面跟着分号。陈述-真实-任何陈述%28通常是一个复合语句%29,如果条件计算为true
陈述-假-任何陈述%28通常是一个复合语句%29,如果条件计算为false
解释
如果条件产生true
转换为bool
,语句-true
被执行。
如果if语句的其他部分存在并且条件产生false
转换为bool
,语句-false
被执行。
在if语句%28的第二种形式中,包括Other%29在内的if语句-true也是if语句,那么该内部if语句也必须包含%28,换句话说,在嵌套的if-语句中,如果%27T有一个其他的%29,则该语句与最接近的语句相关联。
二次
#include <iostream>
int main() {
// simple if-statement with an else clause
int i = 2;
if (i > 2) {
std::cout << i << " is greater than 2\n";
} else {
std::cout << i << " is not greater than 2\n";
}
// nested if-statement
int j = 1;
if (i > 1)
if (j > 2)
std::cout << i << " > 1 and " << j << " > 2\n";
else // this else is part of if (j > 2), not of if (i > 1)
std::cout << i << " > 1 and " << j << " <= 2\n";
// declarations can be used as conditions with dynamic_cast
struct Base {
virtual ~Base() {}
};
struct Derived : Base {
void df() { std::cout << "df()\n"; }
};
Base* bp1 = new Base;
Base* bp2 = new Derived;
if (Derived* p = dynamic_cast<Derived*>(bp1)) // cast fails, returns NULL
p->df( // not executed
if (auto p = dynamic_cast<Derived*>(bp2)) // cast succeeds
p->df( // executed
}
二次
产出:
二次
2 is not greater than 2
2 > 1 and 1 <= 2
df()
二次
If init-statement is used, the if statement is equivalent to. { init_statement if constexpr(optional) ( condition ) statement-true } or. { init_statement if constexpr(optional) ( condition ) statement-true else statement-false } Except that names declared by the init-statement (if init-statement is a declaration) and names declared by condition (if condition is a declaration) are in the same scope, which is also the scope of both statements. std::map | { init_statement if constexpr(optional) ( condition ) statement-true } | | | { init_statement if constexpr(optional) ( condition ) statement-true else statement-false } | | | (since C++17) |
---|---|---|---|---|---|---|---|
{ init_statement if constexpr(optional) ( condition ) statement-true } | | | |||||
{ init_statement if constexpr(optional) ( condition ) statement-true else statement-false } | | |
Constexpr If The statement that begins with if constexpr is known as the constexpr if statement. In a constexpr if statement, the value of condition must be a contextually converted constant expression of type bool. If the value is true, then statement-false is discarded (if present), otherwise, statement-true is discarded. The return statements in a discarded statement do not participate in function return type deduction: template | (since C++17) |
---|
注记
IF语句[医]真实或陈述[医]false不是一个复合语句,它被当作是:
二次
if (x)
int i;
// i is no longer in scope
二次
和。
二次
if (x) {
int i;
} // i is no longer in scope
二次
按条件引入的名称范围(如果是声明)是两个语句%27的合并范围:
二次
if (int x = f()) {
int x; // error: redeclaration of x
} else {
int x; // error: redeclaration of x
}
二次
If statement-true is entered by goto or longjmp, statement_false is not executed. | (since C++14) |
---|---|
Switch and goto are not allowed to jump into a branch of constexpr if statement. | (since C++17) |
关键词
if
,,,else
,,,constexpr
...
另见
c IF语句的文件
*。
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。