0
return int_length(len > 0 ? len : 1) 

what is the meaning of the syntax in the brackets, I keep getting confused when reading this code. thanks

9
  • Ternary operator: ternary wiki - a very condensed if-else. Commented Jun 21, 2013 at 14:18
  • 4
    why the serial downvoting? Commented Jun 21, 2013 at 14:19
  • 3
    @Daniel, apparently somebody's thinking that answering a duplicate deserves downvotes. (Hint: it usually does not.) Commented Jun 21, 2013 at 14:20
  • 1
    @DanielDaranas i was. i guess its some regex. Commented Jun 21, 2013 at 14:22
  • 1
    I'm not the downvoter, but the correct term is "conditional operator". See stackoverflow.com/questions/3257229/… :) Commented Jun 21, 2013 at 14:22

4 Answers 4

4

It is a ternary operator. If len>0 is true result of the expression is len else its 1.

if(len > 0) it will return int_length(len);

else it will return int_length(1);

Sign up to request clarification or add additional context in comments.

Comments

3

That's the ternary operator.

It's equivalent to

if (len>0) return int_length(len); else return int_length(1); 

3 Comments

Why the -1 on this? Upvoting.
why -1 on all correct answers? wtf..
Because they're not strictly speaking correct. In this case, the answer gives something equivalent to the complete statement; the poster asked about the actual expression that was an argument to int_length. (Not that I think that's an valid reason to downvote, but it's the only criticism I can think of.)
2

it means

if(len > 0) { return int_length(len); } else { return int_length(1); } 

1 Comment

Except that it's an order of magnitude more readable.
1

That is the ternary conditional operator. Its an "inline if".

It basically is this

int temp; if (len > 0) { temp = len; } else { temp = 1; } int_length(temp); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.