PDA

View Full Version : a quick java syntax question



babacan
December 28th, 2008, 02:21 PM
I am trying to understand a java code however stucked at a point where I don't know what this stands for:


int x = (y > 0) ? 1 : -1;

I will be greatful is someone explains this notation.

cheers.

_duncan_
December 28th, 2008, 02:37 PM
if y is greater than 0 then x gets the value +1, else x = -1.

Ruhe
December 28th, 2008, 03:30 PM
It's called Ternary operation (http://en.wikipedia.org/wiki/Ternary_operation)

slavik
December 28th, 2008, 03:47 PM
I believe it was first seen in C.

Basically, this is a cut down version of the if..else statement. and the result of the statement goes into x. the result being 1 if the condition is true and -1 otherwise.