-1

What does head = head + 1 == array.length ? 0 : head +1; mean? Does ? means if(condition) and : means else? Can someone write out this expression?

M-M
  • 881
  • 2
  • 10
  • 19

1 Answers1

2

It is a ternary operator that is used as an if-else statement. The basic syntax of the ternary operation is:

x = (Condition)?(value if true):(value if false);

For example,

String x = (2>5)?"2 is greater":"5 is greater";

In the above example, the output will be "5 is greater"

In your example,

head = head + 1 == array.length ? 0 : head +1;

It can be written as,

if ((head + 1) == array.length) {
        head = 0;
    } else {
        head = head + 1;
    }
     
Sachin
  • 66
  • 1
  • 3