Need a quick explanation of this statement

length = ( length < LENGTH ? length : LENGTH - 1 )
I remember reading about the ? but can't remember what it meant in this context and can't seem to find it again in my book. The : part is new to me. I think my instructor had us skip a chapter the talked about it. If some one could give me a quick run done of the statement that would be great. Thanks
?: is the ternary operator. Basically, a ? b : c can be read as "if a, b, else c."

For your example, it could be rewritten as:

1
2
3
4
5
if (length < LENGTH) {
    length = length;
} else {
    length = LENGTH - 1;
}


Of course, length = length is useless, so it would be better rewritten as:

1
2
3
if (length >= LENGTH) {
    length = LENGTH - 1;
}


The ternary operator is just a shorthand version.
cool thanks
Topic archived. No new replies allowed.