1

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.

Corrado
  • 23
  • 4
  • the assignemnt operator usually returns a reference to the left side. `a=b`returns a reference to a. Ifi its is convertible to `bool` it can be checked in conditions (non zero `int` convert to true). – MagunRa Mar 07 '16 at 14:24
  • 1
    Related to [c-variable-declaration-in-if-expression](http://stackoverflow.com/questions/7836867/c-variable-declaration-in-if-expression) and [defining-a-variable-in-the-condition-part-of-an-if-statement](http://stackoverflow.com/questions/12655284/defining-a-variable-in-the-condition-part-of-an-if-statement) – Jarod42 Mar 07 '16 at 14:25
  • @MagunRa - there are no assignment operators here. Those `=` signs are **initialization**, not assignment. – Pete Becker Mar 07 '16 at 14:27
  • @Pete Becker , yes thats why it is just a comment not an answer. I am not sure how it works with initialization. – MagunRa Mar 07 '16 at 14:56
  • @BartekBanachewicz Thanks for the _standard_ suggestion. ;-) – Corrado Mar 07 '16 at 15:10

2 Answers2

2

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.

lcs
  • 4,227
  • 17
  • 36
0

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.

IceFire
  • 4,016
  • 2
  • 31
  • 51