-1

Can anyone please explain me this part of a code:

int ret = leftIndex > middleIndex - 1?leftIndex:middleIndex - 1;

I have not seen such a combination yet or putting a condition into integer variable in this way.

Thank you.

Doctor Jane
  • 77
  • 1
  • 5

1 Answers1

0

The ?: is called a Tertiary Operator. It takes the form:

condition ? true_result : false_result

Which translates to:

if(condition){
    true_result;
} else {
    false_result;
}

Your line of code translates into this:

if(leftIndex > middleIndex - 1){
    int ret = leftIndex;
} else {
    int ret = middleIndex - 1;
}
brenden
  • 559
  • 2
  • 16
  • "*Your line of code translates into this*" not quiet right. The definition of `ret` should be before the `if`-clause. – alk Sep 30 '18 at 14:46