0

I know a statement like the following (commas in place of semi-colons) looks odd:

 if(a<b)printf("Hello\n"),a+=5,b/=5,printf("%d,%d",a,b);

But it works perfectly fine and I had read that it's because comma here acts as a sequence point.I can understand this.But I just fail to understand why the following fails then,where I have used a else as well:

  if(a<b)printf("Hi\n"),else printf("Bye\n"),a+=5,b/=5,printf("%d,%d",a,b);

It gives the error expected expression before 'else'.

Why does the second statement gives error?In the first statement, we saw that comma acts as a sequence point.Then why it doesn't act so before else?What's special about the second case that causes error?Here's my full program:

#include<stdio.h>

int main(void)
{
    int a=30,b=45;

    //if(a<b)printf("Hello\n"),a+=5,b/=5,printf("%d,%d",a,b); //Works well
    if(a<b)printf("Hi\n"),else printf("Bye\n"),a+=5,b/=5,printf("%d,%d",a,b); 
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49
  • http://stackoverflow.com/questions/16262983/are-there-sequence-point-issues-with-statements-like-int-a-4-ptr-a-or-x – Rüppell's Vulture May 16 '13 at 08:22
  • 2
    Forget everything Python (or any other non C/C++ language) ever taught you about a comma, which is an **operator** in C and C++. – WhozCraig May 16 '13 at 08:28
  • @WhozCraig Well,the program attends to/evaluates all stuff to the left of the commas and returns only the one after the last comma.But it's being unfair to the `else` above. – Rüppell's Vulture May 16 '13 at 08:30
  • @WhozCraig As per the answers I got on that link,comma,operator or not, is a sequence point unless used to separate function arguments,right? – Rüppell's Vulture May 16 '13 at 08:31
  • @Dayalrai If I change it to `a>b` I get the same error.And by your logic(and what I too used to feel),statements should end with `;` only,but it's not so as comma is a sequence point here.Else we would be needing a `;` after the first `printf()` too. – Rüppell's Vulture May 16 '13 at 08:35
  • 1
    In this case it is more than just a sequence point issue. It is a language issue. The formation of the statements you're presenting break conformance to **C99 §6.8.4 Selection Statements**. – WhozCraig May 16 '13 at 08:38
  • 1
    @WhozCraig Please post it in an answer, along with the quote from the standard. – Rüppell's Vulture May 16 '13 at 08:42
  • @Rüppell'sVulture I guess, that you mistake sequence point with statement. These are two different things and else requires to be preceeded by a statement, not by a sequence point. – Spook May 16 '13 at 08:46
  • 1
    @Rüppell'sVulture Jens actually hit is more succinctly than I could/did, and imho, correctly. The syntax breaks conformance with if-then-else but only because without a finished statement preceding the else it is left just dangling out there by its lonesome. I already up-voted his answer. – WhozCraig May 16 '13 at 08:47

4 Answers4

6

The comma operator expects an expression and the else part of an if else construct ain't an expression. Thus a comma followed by the keyword else is a syntax error.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • +1 a simple as this may seem, it is right imho. the statement-finale of the if-portion is never finished and the comma-chain is presented a statement starting with **else**, thereby invalid. Nice answer, sir. – WhozCraig May 16 '13 at 08:50
  • This may be useful too: http://stackoverflow.com/questions/19132/expression-versus-statement/19224#19224 – Spook May 20 '13 at 11:04
4

Imagine, that:

(a, b)

is translated to:

{
    a;
    return here b;
}

In this case, return here means "insert in place of this expression". And indeed, , acts as a sequence point.

Then,

if (a<b) printf("Hello\n"), a+=5, b/=5, printf("%d,%d",a,b);

would look like:

if (a<b)
{
    printf("Hello\n");
    a += 5;
    b /= 5;
    return here printf("%d, %d", a, b);
}

But

if(a<b)printf("Hi\n"),else printf("Bye\n"),a+=5,b/=5,printf("%d,%d",a,b);

Would look like:

if (a < b)
{
    printf("Hi\n");
    else printf("Bye\n");
    a += 5;
    b /= 5;
    return here printf("%d, %d", a, b);
}

And the else inside does not make any sense. That's why compiler complaints.

In other words, , in C++ is an operator. So by the operator precedence,

if(a<b)printf("Hi\n"),else printf("Bye\n"),a+=5,b/=5,printf("%d,%d",a,b);

is interpreted as

if(a<b) ( printf("Hi\n"),else printf("Bye\n"),a+=5,b/=5,printf("%d,%d",a,b) );

Which clearly violates the C++ syntax rules.

Spook
  • 25,318
  • 18
  • 90
  • 167
0

Look at the correct syntax of IF-ELSE statement. Compiler expects semicolon to know that IF is ended. So you are basically trying to enter sub if-else statement into the first one, but without new if.

if(a>=18 &a <=64)printf("adult\n"); 
else if(a<=17)printf("minor\n"); 
else printf("senior\n");

So, either the curly brackets "{}" indicate the IF region, or if they are missing, IF statement valid until the first semicolon.

0

In 2nd case else is no having matching if, what is happening here is after printf("Hi\n"), when else arrives compiler looks for if i.e else should come only after terminated if statement. so it is giving error.

Daemon
  • 1,575
  • 1
  • 17
  • 37