Why does return 0 or break not work with the comma operator?
Clash Royale CLAN TAG#URR8PPP
up vote
40
down vote
favorite
I can write the code if(1) x++, y++;
instead of if(1) x++; y++;
, but in some cases it does not work (see below). It would be nice if you tell me about this.
int x = 5, y = 10;
if (x == 5) x++, y++; // It works
if (x == 5) x++, return 0; // It shows an error
The same applies to for
loops:
for (int i = 0; i < 1; i++) y++, y += 5; // It works
for (int i = 0; i < 1; i++) y++, break; // Does not work
c++ c comma comma-operator
add a comment |Â
up vote
40
down vote
favorite
I can write the code if(1) x++, y++;
instead of if(1) x++; y++;
, but in some cases it does not work (see below). It would be nice if you tell me about this.
int x = 5, y = 10;
if (x == 5) x++, y++; // It works
if (x == 5) x++, return 0; // It shows an error
The same applies to for
loops:
for (int i = 0; i < 1; i++) y++, y += 5; // It works
for (int i = 0; i < 1; i++) y++, break; // Does not work
c++ c comma comma-operator
47
You need to learn the difference between expressions and statements.
â Some programmer dude
Aug 21 at 6:40
25
Also note that using the comma expression the way that you do tend to make the code harder to read, understand and maintain.
â Some programmer dude
Aug 21 at 6:42
4
Because that's not how the syntax for the comma operator is defined... and that's about it.
â Lundin
Aug 21 at 6:54
5
For the same reason thatint i = break;
does not work.
â Peter A. Schneider
Aug 21 at 15:09
Why do you even want to do this? Just use braces. It's a lot clearer and simpler to understand.
â Nic Hartley
Aug 21 at 20:23
add a comment |Â
up vote
40
down vote
favorite
up vote
40
down vote
favorite
I can write the code if(1) x++, y++;
instead of if(1) x++; y++;
, but in some cases it does not work (see below). It would be nice if you tell me about this.
int x = 5, y = 10;
if (x == 5) x++, y++; // It works
if (x == 5) x++, return 0; // It shows an error
The same applies to for
loops:
for (int i = 0; i < 1; i++) y++, y += 5; // It works
for (int i = 0; i < 1; i++) y++, break; // Does not work
c++ c comma comma-operator
I can write the code if(1) x++, y++;
instead of if(1) x++; y++;
, but in some cases it does not work (see below). It would be nice if you tell me about this.
int x = 5, y = 10;
if (x == 5) x++, y++; // It works
if (x == 5) x++, return 0; // It shows an error
The same applies to for
loops:
for (int i = 0; i < 1; i++) y++, y += 5; // It works
for (int i = 0; i < 1; i++) y++, break; // Does not work
c++ c comma comma-operator
c++ c comma comma-operator
edited Aug 21 at 15:03
Peter Mortensen
13k1983111
13k1983111
asked Aug 21 at 6:38
Nayem
32825
32825
47
You need to learn the difference between expressions and statements.
â Some programmer dude
Aug 21 at 6:40
25
Also note that using the comma expression the way that you do tend to make the code harder to read, understand and maintain.
â Some programmer dude
Aug 21 at 6:42
4
Because that's not how the syntax for the comma operator is defined... and that's about it.
â Lundin
Aug 21 at 6:54
5
For the same reason thatint i = break;
does not work.
â Peter A. Schneider
Aug 21 at 15:09
Why do you even want to do this? Just use braces. It's a lot clearer and simpler to understand.
â Nic Hartley
Aug 21 at 20:23
add a comment |Â
47
You need to learn the difference between expressions and statements.
â Some programmer dude
Aug 21 at 6:40
25
Also note that using the comma expression the way that you do tend to make the code harder to read, understand and maintain.
â Some programmer dude
Aug 21 at 6:42
4
Because that's not how the syntax for the comma operator is defined... and that's about it.
â Lundin
Aug 21 at 6:54
5
For the same reason thatint i = break;
does not work.
â Peter A. Schneider
Aug 21 at 15:09
Why do you even want to do this? Just use braces. It's a lot clearer and simpler to understand.
â Nic Hartley
Aug 21 at 20:23
47
47
You need to learn the difference between expressions and statements.
â Some programmer dude
Aug 21 at 6:40
You need to learn the difference between expressions and statements.
â Some programmer dude
Aug 21 at 6:40
25
25
Also note that using the comma expression the way that you do tend to make the code harder to read, understand and maintain.
â Some programmer dude
Aug 21 at 6:42
Also note that using the comma expression the way that you do tend to make the code harder to read, understand and maintain.
â Some programmer dude
Aug 21 at 6:42
4
4
Because that's not how the syntax for the comma operator is defined... and that's about it.
â Lundin
Aug 21 at 6:54
Because that's not how the syntax for the comma operator is defined... and that's about it.
â Lundin
Aug 21 at 6:54
5
5
For the same reason that
int i = break;
does not work.â Peter A. Schneider
Aug 21 at 15:09
For the same reason that
int i = break;
does not work.â Peter A. Schneider
Aug 21 at 15:09
Why do you even want to do this? Just use braces. It's a lot clearer and simpler to understand.
â Nic Hartley
Aug 21 at 20:23
Why do you even want to do this? Just use braces. It's a lot clearer and simpler to understand.
â Nic Hartley
Aug 21 at 20:23
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
60
down vote
That's because return
and break
are statements, not expressions. As such, you cannot use it in another expression in any way. if
and the others are similarly also statements.
What you can do however is rewrite your expression (for return
) so that it's not nested in an expression - not that I recommend writing code like that:
return x++, 0;
You can't do that for break
because it doesn't accept an expression.
31
A nice illustration: there is no appreciable syntactic difference betweenx++ / 7
andx++, 7
(both/
and,
being operators). By the same token,x++, break
makes as much sense asx++ / break
: none at all.
â Amadan
Aug 21 at 6:47
1
@Amadan Nice illustration! I'm sure going to use it next time I have to explain this :) Thanks
â Rakete1111
Aug 21 at 6:49
3
@Amadan There's a highly significant difference (at least before C++17, not sure about the new rules): the comma operator induces a happens-before relationship (or in old terms, introduces a sequence point), which means thatx++, x++
is defined, whilex++ / x++
is undefined.
â Sebastian Redl
Aug 21 at 7:56
5
@SebastianRedl: There's also the fact that,
returns the second operand, but/
divides things, which I'd say is a slightly more significant difference :D . Syntactically though, the only thing that makes them different is precedence (simplified here, more realistic here).
â Amadan
Aug 21 at 8:05
2
BUT dont' do it please
â edc65
Aug 21 at 13:39
 |Â
show 2 more comments
up vote
11
down vote
The comma operator is for expressions.
The return
statement and other pure statements are not expressions.
And the conclusion?
â Peter Mortensen
Aug 21 at 15:03
2
@PeterMortensen I answered the "Why?" question, didn't I? What is your point?
â Yunnosch
Aug 21 at 15:48
add a comment |Â
up vote
3
down vote
The comma operator is a binary operator that takes two values. In this way it is the same as +
or *
. Whereas +
adds two values and returns the result, and *
multiplies two values and returns the result, the comma operator simply ignores the value to the left and returns the value on the right.
2 + 5
has value 7
2 * 5
has value 10
2 , 5
has value 5
, simply the operand to the right of the operator.
And so you can't write 2,break
for the same reason that you can't write 2+break
.
Because break is a statement, not a value.
What use is a binary operator that ignores one of its operands? The comma operator ignores the value of the left operand, but the expression is still evaluated.
Any side-effects of that expression are still realized. Consider:
i = 2;
j = 5;
i++, j++;
First the two expressions are evaluated. i++
returns the value 2
, and then increments i
. j++
returns the value 5
, and then increments j
. Finally the comma operator is applied to these two values: 2,5
which ignores the 2
and returns the 5
.
I don't think it ignores the value on the left, but actually evaluates the expression on the left and returns the expression on the right.
â Thomas Matthews
Aug 21 at 18:42
1
Yes, it evaluates the expression, ... and then ignores its value. I've edited to make it clearer. Thanks.
â David Dubois
Aug 21 at 19:23
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
60
down vote
That's because return
and break
are statements, not expressions. As such, you cannot use it in another expression in any way. if
and the others are similarly also statements.
What you can do however is rewrite your expression (for return
) so that it's not nested in an expression - not that I recommend writing code like that:
return x++, 0;
You can't do that for break
because it doesn't accept an expression.
31
A nice illustration: there is no appreciable syntactic difference betweenx++ / 7
andx++, 7
(both/
and,
being operators). By the same token,x++, break
makes as much sense asx++ / break
: none at all.
â Amadan
Aug 21 at 6:47
1
@Amadan Nice illustration! I'm sure going to use it next time I have to explain this :) Thanks
â Rakete1111
Aug 21 at 6:49
3
@Amadan There's a highly significant difference (at least before C++17, not sure about the new rules): the comma operator induces a happens-before relationship (or in old terms, introduces a sequence point), which means thatx++, x++
is defined, whilex++ / x++
is undefined.
â Sebastian Redl
Aug 21 at 7:56
5
@SebastianRedl: There's also the fact that,
returns the second operand, but/
divides things, which I'd say is a slightly more significant difference :D . Syntactically though, the only thing that makes them different is precedence (simplified here, more realistic here).
â Amadan
Aug 21 at 8:05
2
BUT dont' do it please
â edc65
Aug 21 at 13:39
 |Â
show 2 more comments
up vote
60
down vote
That's because return
and break
are statements, not expressions. As such, you cannot use it in another expression in any way. if
and the others are similarly also statements.
What you can do however is rewrite your expression (for return
) so that it's not nested in an expression - not that I recommend writing code like that:
return x++, 0;
You can't do that for break
because it doesn't accept an expression.
31
A nice illustration: there is no appreciable syntactic difference betweenx++ / 7
andx++, 7
(both/
and,
being operators). By the same token,x++, break
makes as much sense asx++ / break
: none at all.
â Amadan
Aug 21 at 6:47
1
@Amadan Nice illustration! I'm sure going to use it next time I have to explain this :) Thanks
â Rakete1111
Aug 21 at 6:49
3
@Amadan There's a highly significant difference (at least before C++17, not sure about the new rules): the comma operator induces a happens-before relationship (or in old terms, introduces a sequence point), which means thatx++, x++
is defined, whilex++ / x++
is undefined.
â Sebastian Redl
Aug 21 at 7:56
5
@SebastianRedl: There's also the fact that,
returns the second operand, but/
divides things, which I'd say is a slightly more significant difference :D . Syntactically though, the only thing that makes them different is precedence (simplified here, more realistic here).
â Amadan
Aug 21 at 8:05
2
BUT dont' do it please
â edc65
Aug 21 at 13:39
 |Â
show 2 more comments
up vote
60
down vote
up vote
60
down vote
That's because return
and break
are statements, not expressions. As such, you cannot use it in another expression in any way. if
and the others are similarly also statements.
What you can do however is rewrite your expression (for return
) so that it's not nested in an expression - not that I recommend writing code like that:
return x++, 0;
You can't do that for break
because it doesn't accept an expression.
That's because return
and break
are statements, not expressions. As such, you cannot use it in another expression in any way. if
and the others are similarly also statements.
What you can do however is rewrite your expression (for return
) so that it's not nested in an expression - not that I recommend writing code like that:
return x++, 0;
You can't do that for break
because it doesn't accept an expression.
edited Aug 21 at 13:05
answered Aug 21 at 6:41
Rakete1111
32.6k975110
32.6k975110
31
A nice illustration: there is no appreciable syntactic difference betweenx++ / 7
andx++, 7
(both/
and,
being operators). By the same token,x++, break
makes as much sense asx++ / break
: none at all.
â Amadan
Aug 21 at 6:47
1
@Amadan Nice illustration! I'm sure going to use it next time I have to explain this :) Thanks
â Rakete1111
Aug 21 at 6:49
3
@Amadan There's a highly significant difference (at least before C++17, not sure about the new rules): the comma operator induces a happens-before relationship (or in old terms, introduces a sequence point), which means thatx++, x++
is defined, whilex++ / x++
is undefined.
â Sebastian Redl
Aug 21 at 7:56
5
@SebastianRedl: There's also the fact that,
returns the second operand, but/
divides things, which I'd say is a slightly more significant difference :D . Syntactically though, the only thing that makes them different is precedence (simplified here, more realistic here).
â Amadan
Aug 21 at 8:05
2
BUT dont' do it please
â edc65
Aug 21 at 13:39
 |Â
show 2 more comments
31
A nice illustration: there is no appreciable syntactic difference betweenx++ / 7
andx++, 7
(both/
and,
being operators). By the same token,x++, break
makes as much sense asx++ / break
: none at all.
â Amadan
Aug 21 at 6:47
1
@Amadan Nice illustration! I'm sure going to use it next time I have to explain this :) Thanks
â Rakete1111
Aug 21 at 6:49
3
@Amadan There's a highly significant difference (at least before C++17, not sure about the new rules): the comma operator induces a happens-before relationship (or in old terms, introduces a sequence point), which means thatx++, x++
is defined, whilex++ / x++
is undefined.
â Sebastian Redl
Aug 21 at 7:56
5
@SebastianRedl: There's also the fact that,
returns the second operand, but/
divides things, which I'd say is a slightly more significant difference :D . Syntactically though, the only thing that makes them different is precedence (simplified here, more realistic here).
â Amadan
Aug 21 at 8:05
2
BUT dont' do it please
â edc65
Aug 21 at 13:39
31
31
A nice illustration: there is no appreciable syntactic difference between
x++ / 7
and x++, 7
(both /
and ,
being operators). By the same token, x++, break
makes as much sense as x++ / break
: none at all.â Amadan
Aug 21 at 6:47
A nice illustration: there is no appreciable syntactic difference between
x++ / 7
and x++, 7
(both /
and ,
being operators). By the same token, x++, break
makes as much sense as x++ / break
: none at all.â Amadan
Aug 21 at 6:47
1
1
@Amadan Nice illustration! I'm sure going to use it next time I have to explain this :) Thanks
â Rakete1111
Aug 21 at 6:49
@Amadan Nice illustration! I'm sure going to use it next time I have to explain this :) Thanks
â Rakete1111
Aug 21 at 6:49
3
3
@Amadan There's a highly significant difference (at least before C++17, not sure about the new rules): the comma operator induces a happens-before relationship (or in old terms, introduces a sequence point), which means that
x++, x++
is defined, while x++ / x++
is undefined.â Sebastian Redl
Aug 21 at 7:56
@Amadan There's a highly significant difference (at least before C++17, not sure about the new rules): the comma operator induces a happens-before relationship (or in old terms, introduces a sequence point), which means that
x++, x++
is defined, while x++ / x++
is undefined.â Sebastian Redl
Aug 21 at 7:56
5
5
@SebastianRedl: There's also the fact that
,
returns the second operand, but /
divides things, which I'd say is a slightly more significant difference :D . Syntactically though, the only thing that makes them different is precedence (simplified here, more realistic here).â Amadan
Aug 21 at 8:05
@SebastianRedl: There's also the fact that
,
returns the second operand, but /
divides things, which I'd say is a slightly more significant difference :D . Syntactically though, the only thing that makes them different is precedence (simplified here, more realistic here).â Amadan
Aug 21 at 8:05
2
2
BUT dont' do it please
â edc65
Aug 21 at 13:39
BUT dont' do it please
â edc65
Aug 21 at 13:39
 |Â
show 2 more comments
up vote
11
down vote
The comma operator is for expressions.
The return
statement and other pure statements are not expressions.
And the conclusion?
â Peter Mortensen
Aug 21 at 15:03
2
@PeterMortensen I answered the "Why?" question, didn't I? What is your point?
â Yunnosch
Aug 21 at 15:48
add a comment |Â
up vote
11
down vote
The comma operator is for expressions.
The return
statement and other pure statements are not expressions.
And the conclusion?
â Peter Mortensen
Aug 21 at 15:03
2
@PeterMortensen I answered the "Why?" question, didn't I? What is your point?
â Yunnosch
Aug 21 at 15:48
add a comment |Â
up vote
11
down vote
up vote
11
down vote
The comma operator is for expressions.
The return
statement and other pure statements are not expressions.
The comma operator is for expressions.
The return
statement and other pure statements are not expressions.
edited Aug 21 at 10:46
Konrad Rudolph
385k997591010
385k997591010
answered Aug 21 at 6:40
Yunnosch
10.8k51833
10.8k51833
And the conclusion?
â Peter Mortensen
Aug 21 at 15:03
2
@PeterMortensen I answered the "Why?" question, didn't I? What is your point?
â Yunnosch
Aug 21 at 15:48
add a comment |Â
And the conclusion?
â Peter Mortensen
Aug 21 at 15:03
2
@PeterMortensen I answered the "Why?" question, didn't I? What is your point?
â Yunnosch
Aug 21 at 15:48
And the conclusion?
â Peter Mortensen
Aug 21 at 15:03
And the conclusion?
â Peter Mortensen
Aug 21 at 15:03
2
2
@PeterMortensen I answered the "Why?" question, didn't I? What is your point?
â Yunnosch
Aug 21 at 15:48
@PeterMortensen I answered the "Why?" question, didn't I? What is your point?
â Yunnosch
Aug 21 at 15:48
add a comment |Â
up vote
3
down vote
The comma operator is a binary operator that takes two values. In this way it is the same as +
or *
. Whereas +
adds two values and returns the result, and *
multiplies two values and returns the result, the comma operator simply ignores the value to the left and returns the value on the right.
2 + 5
has value 7
2 * 5
has value 10
2 , 5
has value 5
, simply the operand to the right of the operator.
And so you can't write 2,break
for the same reason that you can't write 2+break
.
Because break is a statement, not a value.
What use is a binary operator that ignores one of its operands? The comma operator ignores the value of the left operand, but the expression is still evaluated.
Any side-effects of that expression are still realized. Consider:
i = 2;
j = 5;
i++, j++;
First the two expressions are evaluated. i++
returns the value 2
, and then increments i
. j++
returns the value 5
, and then increments j
. Finally the comma operator is applied to these two values: 2,5
which ignores the 2
and returns the 5
.
I don't think it ignores the value on the left, but actually evaluates the expression on the left and returns the expression on the right.
â Thomas Matthews
Aug 21 at 18:42
1
Yes, it evaluates the expression, ... and then ignores its value. I've edited to make it clearer. Thanks.
â David Dubois
Aug 21 at 19:23
add a comment |Â
up vote
3
down vote
The comma operator is a binary operator that takes two values. In this way it is the same as +
or *
. Whereas +
adds two values and returns the result, and *
multiplies two values and returns the result, the comma operator simply ignores the value to the left and returns the value on the right.
2 + 5
has value 7
2 * 5
has value 10
2 , 5
has value 5
, simply the operand to the right of the operator.
And so you can't write 2,break
for the same reason that you can't write 2+break
.
Because break is a statement, not a value.
What use is a binary operator that ignores one of its operands? The comma operator ignores the value of the left operand, but the expression is still evaluated.
Any side-effects of that expression are still realized. Consider:
i = 2;
j = 5;
i++, j++;
First the two expressions are evaluated. i++
returns the value 2
, and then increments i
. j++
returns the value 5
, and then increments j
. Finally the comma operator is applied to these two values: 2,5
which ignores the 2
and returns the 5
.
I don't think it ignores the value on the left, but actually evaluates the expression on the left and returns the expression on the right.
â Thomas Matthews
Aug 21 at 18:42
1
Yes, it evaluates the expression, ... and then ignores its value. I've edited to make it clearer. Thanks.
â David Dubois
Aug 21 at 19:23
add a comment |Â
up vote
3
down vote
up vote
3
down vote
The comma operator is a binary operator that takes two values. In this way it is the same as +
or *
. Whereas +
adds two values and returns the result, and *
multiplies two values and returns the result, the comma operator simply ignores the value to the left and returns the value on the right.
2 + 5
has value 7
2 * 5
has value 10
2 , 5
has value 5
, simply the operand to the right of the operator.
And so you can't write 2,break
for the same reason that you can't write 2+break
.
Because break is a statement, not a value.
What use is a binary operator that ignores one of its operands? The comma operator ignores the value of the left operand, but the expression is still evaluated.
Any side-effects of that expression are still realized. Consider:
i = 2;
j = 5;
i++, j++;
First the two expressions are evaluated. i++
returns the value 2
, and then increments i
. j++
returns the value 5
, and then increments j
. Finally the comma operator is applied to these two values: 2,5
which ignores the 2
and returns the 5
.
The comma operator is a binary operator that takes two values. In this way it is the same as +
or *
. Whereas +
adds two values and returns the result, and *
multiplies two values and returns the result, the comma operator simply ignores the value to the left and returns the value on the right.
2 + 5
has value 7
2 * 5
has value 10
2 , 5
has value 5
, simply the operand to the right of the operator.
And so you can't write 2,break
for the same reason that you can't write 2+break
.
Because break is a statement, not a value.
What use is a binary operator that ignores one of its operands? The comma operator ignores the value of the left operand, but the expression is still evaluated.
Any side-effects of that expression are still realized. Consider:
i = 2;
j = 5;
i++, j++;
First the two expressions are evaluated. i++
returns the value 2
, and then increments i
. j++
returns the value 5
, and then increments j
. Finally the comma operator is applied to these two values: 2,5
which ignores the 2
and returns the 5
.
edited Aug 21 at 18:54
answered Aug 21 at 18:40
David Dubois
2,42111130
2,42111130
I don't think it ignores the value on the left, but actually evaluates the expression on the left and returns the expression on the right.
â Thomas Matthews
Aug 21 at 18:42
1
Yes, it evaluates the expression, ... and then ignores its value. I've edited to make it clearer. Thanks.
â David Dubois
Aug 21 at 19:23
add a comment |Â
I don't think it ignores the value on the left, but actually evaluates the expression on the left and returns the expression on the right.
â Thomas Matthews
Aug 21 at 18:42
1
Yes, it evaluates the expression, ... and then ignores its value. I've edited to make it clearer. Thanks.
â David Dubois
Aug 21 at 19:23
I don't think it ignores the value on the left, but actually evaluates the expression on the left and returns the expression on the right.
â Thomas Matthews
Aug 21 at 18:42
I don't think it ignores the value on the left, but actually evaluates the expression on the left and returns the expression on the right.
â Thomas Matthews
Aug 21 at 18:42
1
1
Yes, it evaluates the expression, ... and then ignores its value. I've edited to make it clearer. Thanks.
â David Dubois
Aug 21 at 19:23
Yes, it evaluates the expression, ... and then ignores its value. I've edited to make it clearer. Thanks.
â David Dubois
Aug 21 at 19:23
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51942943%2fwhy-does-return-0-or-break-not-work-with-the-comma-operator%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
47
You need to learn the difference between expressions and statements.
â Some programmer dude
Aug 21 at 6:40
25
Also note that using the comma expression the way that you do tend to make the code harder to read, understand and maintain.
â Some programmer dude
Aug 21 at 6:42
4
Because that's not how the syntax for the comma operator is defined... and that's about it.
â Lundin
Aug 21 at 6:54
5
For the same reason that
int i = break;
does not work.â Peter A. Schneider
Aug 21 at 15:09
Why do you even want to do this? Just use braces. It's a lot clearer and simpler to understand.
â Nic Hartley
Aug 21 at 20:23