Jens Marder wrote:
"David Harmon" <so****@netcom.comschrieb im Newsbeitrag
news:45****************@news.west.earthlink.net...
>>On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
<pe********@gmail.comwrote,
>>>if( int x=foo() ) {
...
}else{
...
}
but, for example, how can i check that x>50 in the "if" statement? So,
the if will be true only if x>50.
Write it like:
int x = foo();
if( x < 50 ) {
...
With no looping, there's nothing about "If" to keep you from
splitting it into two more understandable statements like that.
Why not use parantheses?
if( (int x=foo()) 50 ) {
...
}else{
...
}
That doesn't compile for me with g++. It seems that the 'int' has to
come before the second '('. However
#include <iostream>
using namespace std;
int foo() { return 100; }
int main(int argc, char *argv[]){
if (int x=foo() 50) {
cout << "condition true" << endl;
cout << "x == " << x << endl;
} else {
cout << "condition false" << endl;
cout << "x == " << x << endl;
}
}
does compile and seems to work as desired.
(int x= foo() 50) and (int x= (foo() 50)) seem to be equivalent in
this context.
(int x=foo() && (x>50)) seems to be legal but doesn't have the desired
meaning. '&&' has higher precedence than '=', so that the assignment
expression computes x in terms of its uninitialized self so that the
result is indeterminate.