Conditional operator with comma

I ran the following program and if value = 0 both of the decrement's on the right of the comma are executed, But if value = 1 only the first increment on the right of the comma is executed. After swapping the incement's and decrement's i noticed that the second operator has no effect.
Why does ++x work and ++y does'nt?

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    int value = 1;
    int x = 1;
    int y = 1;
    value ? ++x, ++y : --x, --y;
    std::cout << x << " " << y;
}
++y does work. The problem is that --y also works.

Your code is equivalent to:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    int value = 1;
    int x = 1;
    int y = 1;
    (value ? ++x, ++y : --x), --y;
    std::cout << x << " " << y;
}
Your line consist of two "top-level" expressions:
1. value ? ++x, ++y : --x
2. --y

value = 1:
1. ++x
2

2. ++y
2

3. --y
1


value = 0:
1. --x
0

2. --y
0
I'm still cant see the logic of line 8 in the original program i posted in which the output is: 1 0 (zero).

when the program is modified:

#include <iostream>

1
2
3
4
5
6
7
8
int main()
{
    int value = 0;
    int x = 1;
    int y = 1;
    value ? --x, --y : ++x, ++y;
    std::cout << x << " " << y;
}


The output is 2 2.

But if value = 1

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    int value = 1;
    int x = 1;
    int y = 1;
    value ? --x, --y : ++x, ++y;
    std::cout << x << " " << y;
}


The output is 0 1.

I read that only the expressions on the left OR the right of the colon will be executed. If that is right, Why is only one operator on the left being executed (the first operator).
Or, As it has been said, Why has both operator's on the left been executed, As well as one from the right of the colon.
The grammar of the conditional ooperator is

logical-or-expression ? expression : assignment-expression


As you see after ':' assignment-expression is followed.
Assignment-expression has a higher priority compared with the comma operator. So if you have for example the following expression

false ? expression1 : ++x, ++y;

then it is equivalent to the following record

( false ? expression1 : ++x ), ++y;

As you see the expression ++y will be executed in any case irrespective of whether the first value before the question mark is false or true.
Let consider your original code snip

1
2
3
4
5
6
7
8
{
	int value = 1;
	int x = 1;
	int y = 1;

	value ? ++x, ++y : --x, --y;
	std::cout << x << " " << y;
}

It can be rewritten as

1
2
3
4
5
6
7
8
{
	int value = 1;
	int x = 1;
	int y = 1;

	( value ? ++x, ++y : --x ), --y;
	std::cout << x << " " << y;
}


As value is equal to 1 then ++x and ++y are executed. x becomes equal to 2 and y also becomes equal to 2. But after the conditional operator there is --y (the comma ooperator). So y becomes equal to 1. And the output confirms this

2 1


Now change the initial value of 'value' to 0. Then we will get

1
2
3
4
5
6
7
8
{
	int value = 0;
	int x = 1;
	int y = 1;

	( value ? ++x, ++y : --x ), --y;
	std::cout << x << " " << y;
}




In this case at first --x will be executed and x will be equal to 0 and after that --y will be executed and y will be equal to 0.

0 0


So --y will be executed in any case because it is executed after the conditional operator due to operators' priority.

You could rewrite your code snip the following way


1
2
3
4
5
6
7
8
{
	int value = 1;
	int x = 1;
	int y = 1;

	value ? ++x, ++y : ( --x , --y );
	std::cout << x << " " << y;
}


In this case --y (together with x) would be executed only in the case when 'value is equal to 0.
Last edited on
Got it at last :) Thank's for the explanation vlad from moscow.
Topic archived. No new replies allowed.