0

Possible Duplicate:
What does '?' do in C++?

xo = ((temp.npieces%2)==1)? 1 : 2;

I can't figure out what this means in it's entirety.

xo is an integer (I think simply determining the difference between a naught and a cross)

temp is an instance of position which stores information about a naughts and crosses board (positions on the board of the naughts and crosses, the state of the game (i.e. whether it has been won, lost, or is undecided), and the number of pieces placed so far upon the board

npieces stores the number of pieces placed so far

%2 is obviously determining the remainder of npieces divided by 2, in order to determine whether this is odd or even, and thus whether naughts or crosses plays next

It's the piece at the end that confuses me, the ? 1 : 2 bit, I'm sure I've seen this or things like this before, but I'm afraid I'm not sure what this does. I know this is likely pretty basic stuff, but I was quite ill in my first year of University (I'm in my second year now) and missed odd fragments so I have irritatingly random, often embarassing, gaps in my knowledge.

(This is part of a mass of code for an assignment I'm working on - trying to make sense of a few hundred lines of code, this included. As this is an assignment, I am more than happy not to be told exactly what this line means - and indeed just to be pointed in the direction of a resource that explains ? 1 : 2 so that I can work it out for myself. Either an online resource, or a larger chapter in any textbook available in Safari Books Online would be very helpful, or, of course, a very brief explanation here.)

Thanks.

And my apologies for the un-specific question title, without knowing what the unfamiliar code does or is called, I couldn't think of a better way to phrase it.

Community
  • 1
  • 1
Eilidh
  • 1,354
  • 5
  • 21
  • 43
  • 1
    It could be very well written as `xo = 2 - (temp.npieces & 1);` – ruslik Dec 03 '10 at 14:35
  • 1
    @ruslik: If the intent is to do a modulo, I believe `2 - (temp.npieces % 2)` to be clearer. This kind of trivial optimization is done by the compiler anyway and tend to make the code harder to read. – ereOn Dec 03 '10 at 14:44
  • The truth is that the ternary operator is hard to google without knowing the keyword apriori. – ruslik Dec 03 '10 at 14:54
  • Yeah I've got to say, once I knew what it meant, it did seem to be an unnecessarily unclear version of `2 - (temp.npieces % 2)` – Eilidh Dec 03 '10 at 15:12

7 Answers7

7
xo = ((temp.npieces%2)==1)? 1 : 2;

if temp.npieces is Even (therefore division remainder is 0) xo is now equal to 2 other wise xo is now equal to 1;

Edit : It does not check if it is Odd, it checks whether the remainder is positive 1.

X ? A : B 

is another way of writing

if(X)
{
    A
}else
{
    B
}

Edit2 :

The purpose of the conditional operator is not to be faster, or more consise. The conditional operator returns a value. if does not. That's the main difference. Therefore x ? A : B isn't exactly the same as if( x ) { A; } else { B; }

Credit @ John Dibling


in C Something is true if it is not equal to 0. (even if it is -145)

  • So it's like a very shortened version of an if statement? – Eilidh Dec 03 '10 at 14:28
  • yes. I don't know if it is faster though. Someone told me that, but I am not sure. –  Dec 03 '10 at 14:29
  • 1
    `?` is determining it's an 'if', `1 : 2` is effectively `answeriftrue : answeriffalse`? – Eilidh Dec 03 '10 at 14:29
  • 1
    In general (condition) ? (condition met) : (otherwise) is ternary operator. – Tom Dec 03 '10 at 14:29
  • 3
    @Muggen: The ternary operator is simply syntactic salt, and brings no benefit speedwise. Only additional questions on SO. – Schedler Dec 03 '10 at 14:33
  • 1
    +1 for mentioning that the modulo may also return -1. Oh wait, you didn't mention it... But you almost did. (-1%2==-1) – xtofl Dec 03 '10 at 15:17
  • 1
    @Schedler: The ternary operator (not really called that, its just the only operator in C++ that takes 3 operands) is sometimes the best or even the only way to do something. Consider for example RAII where youwant to get either one thing or another depending on runtime conditions. – John Dibling Dec 03 '10 at 15:30
  • 1
    The purpose of the conditional operator is not to be faster, or more consise. The conditional operator returns a value. `if` does not. That's the main difference. Therefore `x ? A : B` isn't exactly the same as `if( x ) { A; } else { B; }` – John Dibling Dec 03 '10 at 15:31
  • Edited answer with your comments. –  Dec 03 '10 at 15:36
  • @John Dibling: Even if the conditional operator returns a value, this is nothing that the normal `if-else` construct could not do. Hence the conditional operation is only syntactic in nature. With the `if-else` construct one naturally has to introduce a temporary variable, but this only makes things explicit, which is a good thing IMHO. – Schedler Dec 03 '10 at 16:45
  • You can't do RAII using an `if` without either creating temporaries or pointers. If the RAII object is expensive, you may not want to do that. – John Dibling Dec 03 '10 at 16:50
5
bool ? 1 : 2

is called the ternary operator. If bool is true, 1 is returned, otherwise 2.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • 1
    It's not really called the ternary operator. It's just the only operator in C++ that takes 3 operands. It's actually called the conditional operator. The name "ternary operator" is colloquial. – John Dibling Dec 03 '10 at 16:34
2

Ternary operator

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2
xo = ((temp.npieces%2)==1)? 1 : 2;

is equivalent to:

if((temp.npieces%2) == 1)
    xo = 1;
else
    xo = 2;
BlackBear
  • 22,411
  • 10
  • 48
  • 86
2

This is a conditional expression.

It works like that:

  1. (temp.npieces%2)==1 is evaluated. This expression is true if temp.npieces is odd (and greater than 0).
  2. If the just evaluated value is true, the result is the first part after ?, i.e., 1
  3. Otherwise, the result is the last part, i.e., 2.

You can see this code as a shorthand for

if ((temp.npieces%2)==1)
    xo = 1;
else
    xo = 2;

Please note that the condition (temp.npieces%2)==1 does not really test if temp.npieces is odd. For negative values of temp.npieces we have (temp.npieces%2)==-1. So if temp.npieces is not unsigned, this code might have a problem.

Vlad
  • 35,022
  • 6
  • 77
  • 199
1

read it as this:

if ((temp.npieces%2)==1))
 xo = 1;
else
 xo = 2;

if npieces is odd affect 1 to xo else affect 2

MiniScalope
  • 1,429
  • 1
  • 16
  • 22
1

You are looking at a ternary operator. Something of the form

variable = condition ? value-if-true : value-if-false

is a shorthand for something like

if (condition) variable = value-if-true else variable = value-if-false

Now ((temp.npieces%2)==1) is going to be true when temp.npieces is odd.

It does have the feel of a secret decoder ring, but the ternary operator has been around since before C had the ++.

rajah9
  • 11,645
  • 5
  • 44
  • 57