Go Down

Topic: Need help understanding a line of code... (Read 1 time) previous topic - next topic

Krupski


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).  
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

madvoid


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?

AWOL

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

Go Up