variable declaration in if statement

Discussion in 'C++' started by Denis Petronenko, Aug 5, 2006.

  1. 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.
     
    Denis Petronenko, Aug 5, 2006
    #1
    1. Advertising

  2. Denis Petronenko

    David Harmon Guest

    On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
    <> 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.
     
    David Harmon, Aug 5, 2006
    #2
    1. Advertising

  3. Denis Petronenko

    Jens Marder Guest

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

    Matt Guest

    Jens Marder wrote:
    > "David Harmon" <> schrieb im Newsbeitrag
    > news:...
    >
    >>On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
    >><> 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.
     
    Matt, Aug 6, 2006
    #6
  7. * 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?
     
    Alf P. Steinbach, Aug 6, 2006
    #7
  8. Denis Petronenko

    Matt Guest

    Matt wrote:
    > Jens Marder wrote:
    >
    >> "David Harmon" <> schrieb im Newsbeitrag
    >> news:...
    >>
    >>> On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
    >>> <> 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.
     
    Matt, Aug 6, 2006
    #8
  9. Denis Petronenko

    Ian Collins Guest

    Denis Petronenko wrote:
    > David Harmon wrote:
    >
    >>On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
    >><> 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.
     
    Ian Collins, Aug 6, 2006
    #9
  10. Denis Petronenko

    Mark P Guest

    Denis Petronenko wrote:
    > David Harmon wrote:
    >> On 5 Aug 2006 12:09:44 -0700 in comp.lang.c++, "Denis Petronenko"
    >> <> 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 ) { ... }
    }
     
    Mark P, Aug 6, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    969
  2. baumann@pan

    variable definiton / variable declaration

    baumann@pan, May 11, 2005, in forum: C Programming
    Replies:
    3
    Views:
    545
    CBFalconer
    May 11, 2005
  3. Bolin
    Replies:
    4
    Views:
    414
  4. Nicolas Matringe
    Replies:
    5
    Views:
    7,709
    Nicolas Matringe
    Apr 19, 2007
  5. Srinu
    Replies:
    10
    Views:
    958
    David Thompson
    Nov 4, 2007
Loading...

Share This Page