OT: y=y<0?0:y

Started by
5 comments, last by penetrator 22 years, 4 months ago
What does this code mean ? glHorizon_Project

www.web-discovery.net

Advertisement
y = y < 0 ? 0 : y

is the same as:

if (y < 0) { y = 0 } else { y = y }

it''s just a shortcut
That must seriously set some kind of record for "wackiest syntax", it is definitely one of the most confusing (yet simple) things I''ve run across in C .

Anthracks
Anon, you forgot semicolons
it''s called a Ternary operator

syntax:
(expression1) ? (expression2) : (expression3)
"If expression1 is true,
return the value of expression2;
otherwise, return the value of expression3."
The best way I found to remeber it is just like an if statement:

a ? b : c
if a then b else c
Check out NeHe''s site under OpenGL articles, Handy Compilation of C/C++ Methods. It''s where I first heard of it.

http://nehe.gamedev.net/articles/article05.asp

This topic is closed to new replies.

Advertisement