? : and booleans on my assignment

Hi, I'm new to C++ and in my assignment I have these two lines:

(day<=29) ? valid=true : valid=false;
(day<=28) ? valid=true : valid=false;

It is for verifying the days in February.
I've never seen ? and : used like this so I would like some clarification.
Is this saying that if the day<=29/28 then set valid to true, otherwise false?
Would I be correct in assuming that?
Yes, you are correct. That's exactly how the ternary operator works:

 
(some_condition) ? (use_if_true) : (use_if_false)


1
2
3
4
5
6
7
8
9
10
11
// this
(day<=29) ? valid=true : valid=false;

// is the same as this:
if(day <= 29)
    valid = true;
else
    valid = false;

// but of course, a bit more sane way to write it would be like this:
valid = (day <= 29);
Oh I see. Its just written that way in my assignment and I've never seen it before nor was it explained in anything I have read so far. Thanks for the clarification! =D
Topic archived. No new replies allowed.