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

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
user2204993
  • 41
  • 1
  • 2
  • 6

4 Answers4

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);

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
3

That's the ternary operator.

It's equivalent to

if (len>0)
    return int_length(len);
else
    return int_length(1);
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Why the -1 on this? Upvoting. – Captain Skyhawk Jun 21 '13 at 14:20
  • why -1 on all correct answers? wtf.. – Yami Jun 21 '13 at 14:20
  • 1
    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.) – James Kanze Jun 21 '13 at 14:26
2

it means

if(len > 0)
{
   return int_length(len);
}
else
{  
   return int_length(1);
} 
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
Yami
  • 1,405
  • 1
  • 11
  • 16
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);
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445