-3

Consider the following code:

  i = (i == array.length-1) ? 0 : i + 1;

As I understand, the conditional operator works as follows:

booleanCondition ? executeThisPartIfBooleanConditionIsTrue : executeThisPartIfBooleanConditionIsFalse 

What does 0 execute?

Monika
  • 301
  • 3
  • 12

2 Answers2

0

I do not think the "positive conditional test result" really has a formal name.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

?: Ternary (shorthand for if-then-else statement)

using a boolean example

isConditionTrue = 1 == 2 ? true : false;

this in your example

i = (i == array.length-1) ? 0 : i + 1;

has the same result as

if (i == array.length-1)
{i= 0 ;}
else {i = i + 1;}
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
-1

This is known as the Elvis Operator (see [https://en.wikipedia.org/wiki/Elvis_operator]) and given the following example:

x = A ? B : C;

... it means that if A is evaluated to 'true' than x gets assigned the value B otherwise it gets assigned the value C.

In your example it means, that if 'i==array.length-1' then 'i' is set to '0' otherwise 'i' is set to 'i+1'.