tags:

views:

173

answers:

4

can somebody please tell me the difference between the following two code snippets:

//Code snippet A: Compiles fine
int main()
{
    if(int i = 2)
    {
        i = 2 + 3;
    }
    else
    {
        i = 0;
    }
}

//Code Snippet B: Doesn't compile :(
int main()
{
    if((int i = 2))
    {
        i = 2 + 3;
    }
    else
    {
        i = 0;
    }
}

If you notice the diff is just an extra parenthesis at if statement. I am using g++ compiler.Error is "expected primary-expression before âintâ"

+2  A: 

Both look wrong.

Just because code compiles it doesn't mean it's valid. I'm surprised that the first case is compiling, since it seems like i isn't defined in the else case.

edit: I'm wrong here, definitions in the if condition are valid for that whole if / else statement.

therefromhere
The scope of the int declarator is to the end of the *entire* if statement.
paxdiablo
Not only that, but the expression inside the if is performing an assignment not a comparison, and as such the else case will never be reached.
Alex Deem
The first case is perfectly valid C++. It doesn't have to be useful. 'if (0)' is also perfectly valid C++ despite it's uselessnes.
paxdiablo
yeah..compilation and coding these is not an issue ..I was just trying to understand "the ill formed cpp statements".
White_Pawn
..I mean, the snippets are useless and we will never write such codes..but there are few not so known things about c++.
White_Pawn
My mistake, I didn't realise that `if (int i = 0) {} else { i = 1; } ` was valid code.
therefromhere
else is not that useful with literals, but you'd generally :) see things like `if (X* p = foo())`
UncleBens
Makes sense, maybe I knew it, I'd just never thought that through. :)
therefromhere
+15  A: 

Section 6.4 of the C++ standard (draft n2914 of c++0x) has this to say about the format of if statements:

Selection statements choose one of several flows of control.
   selection-statement:
      if ( condition ) statement
      if ( condition ) statement else statement
      switch ( condition ) statement
   condition:
      expression
      type-specifier-seq attribute-specifieropt declarator = initializer-clause
      type-specifier-seq attribute-specifieropt declarator braced-init-list

That bit at the end means a condition can be either an expression or a decalarator-type construct.

And the minute the parser hits that second parenthesis, it becomes an expression, so no declarations allowed, I'm afraid.

The snippet:

if (int i = 2) { ... } else { ... }

is perfectly valid C++ in which the if section defines an integer i for the duration of the if/else and sets it to 2. It then uses that 2 as the input to the if (2 is always true, being non-zero).

The snippet if((int i = 2)) is no different syntactically to int x = (int i = 2;); if (x) which is not valid C++.

paxdiablo
I like your answer as well mate :) You certainly are doing a good job here..
White_Pawn
+4  A: 

Here is another variant of second snippet error:

int main()
{
    int i = (int j = 0);
    return 0;
}

You can't declare variables inside ANY expression. Second () inside for is the same case - you can declare variable i inside for() but not inside nested expression placed into ().

Hope I explained it in proper words, maybe more correct explanation exists. Actually both code fragments are odd.

Roman Nikitchenko
+9  A: 

Snippet A is fine - the if condition delares and initialises a variable that can be interpreted as a boolean. The variable is defined within the if and any else blocks.

Snippet B is wrong, because you can't put parentheses around a declaration - you can only put them round expressions. The following is also wrong for the same reason:

int main()
{
    int i;       // OK
    (i = 2);     // OK

    (int x = 2); // compile error
}
Mike Seymour
Thanks Mike! simple and the best answer :) ..wish i could vote..not registered yet.
White_Pawn
Well, register so you can. Although I can't agree it's the *best* answer, it's certainly a good succinct one, especially for those here not willing to sit through my usual essays to find a gem of good information:-) I've already upvoted it.
paxdiablo
upvoting is to encourage..right? ..and am philanthropist anyways :)
White_Pawn