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,082
  • 9
  • 61
  • 111
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,190
  • 2
  • 29
  • 47
3

That's the ternary operator.

It's equivalent to

if (len>0)
    return int_length(len);
else
    return int_length(1);
Luchian Grigore
  • 245,575
  • 61
  • 446
  • 609
  • 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
  • 119,648
  • 17
  • 200
  • 293
Yami
  • 1,270
  • 1
  • 8
  • 14
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
  • 181,601
  • 45
  • 354
  • 430