Pages: 1 [2]   Go Down
Author Topic: Need help understanding a line of code...  (Read 1065 times)
0 Members and 1 Guest are viewing this topic.
Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 792
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
There's nothing worse than a teacher that can't teach.
Oh come now, really there are lots of things worse.
Degenerative brain disease, sociopaths with assault weapons... the list is nearly endless.

Quote
...which means nothing to a person who doesn't know what it is.
Which is why I put it in quotes so that the OP could cut and paste and answer their own question
Quote
so i can look them up?
then come back here if there was anything they didn't understand.

(I teach SCUBA diving. All my students have survived, so I consider myself quite good at it)

I agree with you especially on the nutcases that shoot up schools. I'm a gun owner and people who do such terrible things make my hobby all the more difficult to enjoy (because all gun owners get lumped into the "psycho murderer" category).  
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Salt Lake City
Offline Offline
Jr. Member
**
Karma: 1
Posts: 69
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes indeed. The question mark denotes the thing that happens if "X" is true and the colon denotes the thing that happens if "X" is false.

Here's an example (pseudo code):

what_is_larger = (moon < sun) ? "The sun" : "The moon"

The test condition is "moon less than sun". Since the moon is smaller than the sun, "(moon < sun)" evaluates to "TRUE". Therefore, the string after the QUESTION MARK is the one that is placed into the variable "what_is_larger".

I could also write it this way:

what_is_larger = (moon > sun) ? "The moon" : "The sun"

Since, in this case, "(moon > sun)" is FALSE, the string after the COLON is placed into the variable "what_is_larger".

Got it?


@Krupkski,@AWOL,

Thank you for your help, it makes perfect sense.  The above explanation was very helpful.  Is there any reason (other than succinctness) to use this as opposed to an if-else statement?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25795
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suppose the main reason is you can embed it in expressions, but you can always code around it and a decent compiler will probably generate the same code.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: 1 [2]   Go Up
Jump to: