在线文档教程
C++
语言 | Language

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 m; std::mutex mx; extern bool shared_flag; // guarded by mx int demo() { if (auto it = m.find(10 it != m.end()) { return it->size( } if (char buf10; std::fgets(buf, 10, stdin)) { m0 += buf; } if (std::lock_guard lock(mx shared_flag) { unsafe_ping( shared_flag = false; } if (int s; int count = ReadBytesWithSignal(&s)) { publish(count raise(s } if (auto keywords = {"if", "for", "while"}; std::any_of(keywords.begin(), keywords.end(), &s { return s == kw; })) { std::cerr << "Token must not be a keyword\n" } }{ 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 auto get_value(T t) { if constexpr (std::is_pointer_v) return *t; // deduces return type to int for T = int* else return t; // deduces return type to int for T = int } The discarded statement can odr-use a variable that is not defined. extern int x; // no definition of x required int f() { if constexpr (true) return 0; else if (x) return x; else return -x; } If a constexpr if statement appears inside a templated entity, and if condition is not value-dependent after instantiation, the discarded statement is not instantiated when the enclosing template is instantiated . template void g(T&& p, Rest&& ...rs) { // ... handle p if constexpr (sizeof...(rs) > 0) g(rs... // never instantiated with an empty argument list. } Note: an example where the condition remains value-dependent after instantiation is a nested template, e.g. template void g() { auto lm = { if constexpr (sizeof(T) == 1 && sizeof p == 1) { // this condition remains value-dependent after instantiation of g } }; } Note: the discarded statement can't be ill-formed for every possible specialization: template void f() { if constexpr (std::is_arithmetic_v) // ... else static_assert(false, "Must be arithmetic" // ill-formed: invalid for every T } The common workaround for such a catch-all statement is a type-dependent expression that is always false: template struct dependent_false : std::false_type {}; template void f() { if constexpr (std::is_arithmetic_v) // ... else static_assert(dependent_false::value, "Must be arithmetic" // ok } Labels (goto targets, case labels, and default:) appearing in a substatement of a constexpr if can only be referenced (by switch or goto) in the same substatement.(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。

http://en.cppreference.com/w/cpp/language/if