Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > variable declaration in if statement

Reply
Thread Tools

variable declaration in if statement

 
 
Denis Petronenko
Guest
Posts: n/a
 
      08-05-2006
i can write something like this

int foo()
{
return 100;
}

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.

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      08-05-2006
On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
<(E-Mail Removed)> wrote,
>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.

 
Reply With Quote
 
 
 
 
Jens Marder
Guest
Posts: n/a
 
      08-05-2006

"David Harmon" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
> <(E-Mail Removed)> wrote,
>>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{
....
}



 
Reply With Quote
 
Denis Petronenko
Guest
Posts: n/a
 
      08-05-2006

David Harmon wrote:
> On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
> <(E-Mail Removed)> wrote,
> >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.


i don't want to use x variable after if statement and want to reduce
scope of x variable to if statement only.

 
Reply With Quote
 
Denis Petronenko
Guest
Posts: n/a
 
      08-05-2006

Jens Marder wrote:
> "David Harmon" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
> > On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
> > <(E-Mail Removed)> wrote,
> >>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{
> ...
> }


because it doesn't compile. at least using this
g++ (GCC) 4.0.4 20060507 (prerelease) (Debian 4.0.3-3)

 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      08-05-2006
Jens Marder wrote:
> "David Harmon" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
>
>>On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
>><(E-Mail Removed)> wrote,
>>
>>>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.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-05-2006
* Jens Marder:
>
> Why not use parantheses?
>
> if( (int x=foo()) > 50 ) {
> ...
> }else{
> ...
> }


Because it doesn't conform to the 'if' statement syntax; it's the same
as you cannot put parentheses around a declaration elsewhere.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      08-05-2006
Matt wrote:
> Jens Marder wrote:
>
>> "David Harmon" <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:(E-Mail Removed)...
>>
>>> On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
>>> <(E-Mail Removed)> wrote,
>>>
>>>> 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.


whoops. x is left with the value of the '>' expression, namely 0 or 1,
so that this doesn't do what the OP wanted.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-05-2006
Denis Petronenko wrote:
> David Harmon wrote:
>
>>On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
>><(E-Mail Removed)> wrote,
>>
>>>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.

>
>
> i don't want to use x variable after if statement and want to reduce
> scope of x variable to if statement only.
>


{
int x = foo();
if( x < 50 ) {
...
}
}
--
Ian Collins.
 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      08-05-2006
Denis Petronenko wrote:
> David Harmon wrote:
>> On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
>> <(E-Mail Removed)> wrote,
>>> 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.

>
> i don't want to use x variable after if statement and want to reduce
> scope of x variable to if statement only.
>


Then put braces around both the variable declaration and the if statement.

{
int x = foo();
if ( x < 50 ) { ... }
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable declaration inside a switch statement. Srinu C Programming 10 11-04-2007 11:02 PM
Using a SQL statement with variable parameters and variable criteria Froefel ASP .Net 1 07-04-2007 09:38 AM
generate statement inside a process (conditional variable declaration) Nicolas Matringe VHDL 5 04-19-2007 07:38 PM
Variable declaration taken as a function pointer declaration Bolin C++ 4 12-02-2005 05:28 PM
variable definiton / variable declaration baumann@pan C Programming 3 05-11-2005 01:01 PM



Advertisments