Ternary and Modulus Operator Confusion

I've been playing around with the code below in order to try to accurately predict its output, but what I'm receiving is the total opposite of what I'd expect so I must be interpreting it incorrectly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

int player = 1;

int main()
{
	do 
	{
		cout << player << endl;
		player++;
		player = (player % 2) ? 7 : 4;
		cin.get();

	} 
	while 
	(
	player < 9
	);
	
	return 0;
};



The output I expect is:
1
7
4
7
4
7
...



Instead I receive:
1
4
7
4
7
4
...



In line 13 I'm using the ternary operator and I'm using the value "7" for when the condition is true and "4" for when it is false. I find it weird that I receive the output in this order because it is the equivalent of if I said:

1
2
3
4
		cout << player << endl;
		player++;
		player = (player % 2 == 1) ? 7 : 4;
		cin.get();
Notice:
(player % 2 == 1)



Does that mean that simply declaring "player %2" is the equivalent of "player %2 == 1" as opposed to "player %2 == 0"?

Thanks in advance for the help.
Last edited on
In C and C++ value of 0 is threated as false, and any other integral number as true.

So player % 2 in condition is threated as player %2 != 0
Last edited on
Wow! I never would have guessed that.

I wish the books I've been reading had mentioned that and I'm going to do a bit of experimenting with it.

Thanks so much MiiNiPaa!
Topic archived. No new replies allowed.