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.