Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3497500
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:22:41+00:00 2026-05-18T12:22:41+00:00

Possible Duplicate: What does '?' do in C++? xo = ((temp.npieces%2)==1)? 1 : 2;

  • 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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:22:41+00:00Added an answer on May 18, 2026 at 12:22 pm
    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What does ‘unsigned temp:3’ means I just found this code in a
Possible Duplicate: What does 'unsigned temp:3' means I don't understand this struct definition. It
Possible Duplicate: What does 'unsigned temp:3' means what does this mean in c int
Possible Duplicate: What does ‘unsigned temp:3’ means I'm learning some kernel code, and came
Possible Duplicate: What does 'unsigned temp:3' means I have been trying to learn raw
Possible Duplicate: What does 'unsigned temp:3' mean? please what does this notation mean int
Possible Duplicate: What does 'unsigned temp:3' means I'm a newbie to c/c++ and I
Possible Duplicate: Returning a reference can work? Why this code works ( matrix is
Possible Duplicate: Does readdir() guarantee an order? I'm guessing this isn't the case, and
Possible Duplicate: What does this “(function(){});”, a function inside brackets, mean in javascript ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.