简体   繁体   English

变量定义在c ++中的控件结构中起什么作用?

[英]What does variable definition do in control structure in c++?

What does variable defintion do if I use it as the control structure of the if , while , for statements? 如果将变量定义用作ifwhilefor语句的控制结构,该怎么做?

Consider these two fragments of code from C++ Primer(5th Edition) : 考虑一下C ++ Primer(第5版)的以下两个代码片段:

    while (int i = get_num())  //i is created and initialized on each iteration
    cout << i << endl;

and

    while(bool status = find(word)) {/*...*/}  //assume that find(word) returns a bool type

I do not know whether variable definition "returns" a bool type to indicate the success of the definition,or variable definition returns the variable itself when used as the condition of control structure. 我不知道变量定义是否“返回” bool类型以指示定义是否成功,或者当用作控制结构的条件时,变量定义会返回变量本身。 And I think the second fragment works fine,for status is the result of the = operator.The condition tests whether status is true . 而且我认为第二个片段可以正常工作,因为status=运算符的结果。条件测试status是否为true A friend of mine says the second fragment is in error,for the variable status is undeclared. 我的一个朋友说第二个片段是错误的,因为变量status未声明。

While loops expect a bool expression. While循环期望bool表达式。

while({BOOL}) {...}

In the case of the code above 对于上面的代码

while(bool status = find(word)) {...}

simplifies down to 简化到

while(status) {...}

Status is initialized to the result of find(word) at the start of each execution of the loop. 每次循环执行开始时,状态都会初始化为find(word)的结果。

status is then available within the loop. status然后在循环内可用。

§ 3.3.3 Block Scope 第3.3.3节范围

Names declared in the for-init-statement, the for-range-declaration, and in the condition of if, while, for, and switch statements are local to the if, while, for, or switch statement (including the controlled statement), and shall not be redeclared in a subsequent condition of that statement nor in the outermost block (or, for the if statement, any of the outermost blocks) of the controlled statement; 在for-init-statement,for-range-declaration中以及在if,while,for和switch语句的条件下声明的名称对于if,while,for或switch语句(包括受控语句)是局部的,并且不得在该语句的后续条件中,也不得在受控语句的最外层块(或对于if语句,是任何最外层块)中重新声明;

Regarding your second question: 关于第二个问题:

do not know whether variable definition "returns" a bool type to indicate the success of the definition,or variable definition returns the variable itself when used as the condition of control structure. 不知道变量定义是否“返回”布尔类型以指示定义是否成功,或者变量定义在用作控制结构的条件时会返回变量本身。

As long as the variable is convertible to bool, there is no issue. 只要该变量可转换为bool,就没有问题。

Given 特定

while(Foo x = Expression()) {...}

can be expressed as 可以表示为

while(static_cast<bool>(x)) {...}

as long as Foo is convertible to bool, it can be declared and used in the while conditional. 只要Foo可转换为bool,就可以while有条件的情况下声明和使用它。

The statements are both fine. 这些陈述都很好。

In the first case get_num() returns a value that is assigned to the newly declared variable i. 在第一种情况下,get_num()返回分配给新声明的变量i的值。 ints are evaluated as true if they are not zero and evaluated as false if they are zero. 如果int不为零,则将其评估为true;如果它们为零,则将其评估为false。 So, this loop will run as long as i is not zero. 因此,只要我不为零,该循环就会运行。

In the second statement find seems to return a bool which is assigned to status. 在第二条语句中,find似乎返回分配给状态的布尔值。 As long as status is true, the loop will run. 只要状态为true,循环就会运行。

Within the brackets of while the corresponding variable can be used, ie you can use i in the first loop and status in the second one, which really is the advantage of writing in like that. 在while括号内,可以使用相应的变量,即,可以在第一个循环中使用i,在第二个循环中使用status,这的确是这样做的好处。 However, it does not really make sense in the second code snippet because you already know that status is true... otherwise the loop would just not be executed anymore. 但是,由于您已经知道状态为true,所以在第二个代码段中并没有什么意义,否则循环将不再执行。 And if you change status here this does not work, either, because there will be a new locally declared status variable for each loop run. 而且,如果您在此处更改状态,则也不起作用,因为每次循环运行都会有一个新的本地声明的状态变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM