Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Probably a basic question, but.... (http://www.chiefdelphi.com/forums/showthread.php?t=23961)

Raven_Writer 16-01-2004 13:36

Probably a basic question, but....
 
Ok, I've never known what this code actually does:

PHP Code:

/**  example only **/
   
int hi 0;
   
   return 
hi 0 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

Re: Probably a basic question, but....
 
Quote:

Originally Posted by Raven_Writer
Ok, I've never known what this code actually does:

PHP Code:

/**  example only **/
   
int hi 0;
   
   return 
hi 0 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

Re: Probably a basic question, but....
 
are you sure it's not

Code:

int hi = 0;

return hi ? 1 : 0;

what i've just written is equivalent to this:

Code:

int hi = 0;

if(hi) return 1;
else return 0;

basically, the code
Code:

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

so you could use
Code:

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

Re: Probably a basic question, but....
 
Quote:

Originally Posted by Jay Lundy
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.

Quote:

Originally Posted by deltacoder1020
are you sure it's not
Code:

          int hi = 0;
 
 return hi ? 1 : 0;

what i've just written is equivalent to this:
Code:

          int hi = 0;
 
 if(hi) return 1;
 
 else return 0;

basically, the code
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

Re: Probably a basic question, but....
 
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

Re: Probably a basic question, but....
 
Quote:

Originally Posted by Raven_Writer
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

Re: Probably a basic question, but....
 
Quote:

Originally Posted by gwross
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

Re: Probably a basic question, but....
 
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


All times are GMT -5. The time now is 16:47.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi