Corrado
Corrado

Reputation: 23

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?

Consider these two fragments of code from C++ Primer(5th Edition):

    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. And I think the second fragment works fine,for status is the result of the = operator.The condition tests whether status is true. A friend of mine says the second fragment is in error,for the variable status is undeclared.

Upvotes: 1

Views: 156

Answers (2)

lcs
lcs

Reputation: 4245

While loops expect a bool expression.

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.

status is then available within the loop.

§ 3.3.3 Block Scope

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;

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.

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.

Upvotes: 2

IceFire
IceFire

Reputation: 4147

The statements are both fine.

In the first case get_num() returns a value that is assigned to the newly declared variable i. ints are evaluated as true if they are not zero and evaluated as false if they are zero. 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. As long as status is true, the loop will run.

Within the brackets of while the corresponding variable can be used, i.e. you can use i in the first loop and status in the second one, which really is the advantage of writing in like that. 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. 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.

Upvotes: 0

Related Questions