Log in

View Full Version : Probably a basic question, but....


Raven_Writer
16-01-2004, 13:36
Ok, I've never known what this code actually does:


/** example only **/
int hi = 0;

return hi 0 ? 1 : 0;


Could someone explain that to me please?

My books don't have any explination (sp?) on that.

Jay Lundy
16-01-2004, 13:56
Ok, I've never known what this code actually does:


/** example only **/
int hi = 0;

return hi 0 : 1 ? 0;


Could someone explain that to me please?

My books don't have any explination (sp?) on that. The ? : is called the ternary operator. It's a more compact way of doing if statements. The format is:

conditional statement ? statement executed if true : statement executed if false;

I'm assuming that extra 0 stuck in there after hi is a typo?

deltacoder1020
16-01-2004, 13:56
are you sure it's not


int hi = 0;

return hi ? 1 : 0;


what i've just written is equivalent to this:


int hi = 0;

if(hi) return 1;
else return 0;


basically, the code

a ? b : c;

means "replace this with b is a is true, otherwise, replace this with c"

so you could use

var1 += (x>2 ? 4 : 1);

to increment var1 by 4 if x is greater than 2, but only by 1 if x is less than or equal to 2.

it's more than just a compact way of doing if statements, as i've illustrated - you can't embed a if statement in an expression, whereas you can embed the ternary operator.

Raven_Writer
16-01-2004, 14:01
The ? : is called the ternary operator. It's a more compact way of doing if statements. The format is:

conditional statement ? statement executed if true : statement executed if false;

I'm assuming that extra 0 stuck in there after hi is a typo? Yea, I was typing kinda/pretty fast then.

are you sure it's not

int hi = 0;

return hi ? 1 : 0;


what i've just written is equivalent to this:

int hi = 0;

if(hi) return 1;

else return 0;


basically, the code a ? b : c;

means "replace this with b is a is true, otherwise, replace this with c"
Thanks, that really clears it up (also thanks to you Jay :)).

It might make if-case statements more easier

Raven_Writer
16-01-2004, 18:59
Here's another question....

I was searching through the default code for the tracking. I opened up "ifi_default.h", and saw that in the struct's, it'd have like: "unsigned int pie:1;"

My question now is, does the colon take the place of the equal sign?

Greg Ross
16-01-2004, 20:07
Here's another question....

I was searching through the default code for the tracking. I opened up "ifi_default.h", and saw that in the struct's, it'd have like: "unsigned int pie:1;"

My question now is, does the colon take the place of the equal sign?
The :1 indicates pie occupies a single bit.

Raven_Writer
16-01-2004, 20:51
The :1 indicates pie occupies a single bit.Thank you for clearing that up for me.

I just that it was another way to assign a variable.

deltacoder1020
17-01-2004, 01:04
it basically allows you to save space - you could use each of the 16 bits of an int to store a different 1/0 value, approximating a set of 16 booleans