Skip to content
Advertisement

Is there a difference in the working of AND operator in JAVA and C? [closed]

JavaScript

If j>=0 is placed after the condition temp< arr[j], I’m getting the error as

JavaScript

But in C language this doesn’t happen either I write j>=0 && temp < arr[i] or temp<arr[i] && j>=0.

1st Image Image of Error when j>=0 is placed after temp<arr[j]

2nd Image Image when j>=0 is placed before temp<arr[j]

Advertisement

Answer

No. Its called shortcircuit.

The order can affect your condition because if any of the conditions from left to right fails, it will mark the complete condition as false and doesn’t evaluate any more conditions.

JavaScript

So if j >= 0 is false, it will not evaluate temp < arr[j]

but if you interchange first it will evaluate temp < arr[j] where arr[j] can produce an ArrayIndexOutOfBoundsException in Java.

You can read more about shortCircuit.

In C, it may not give an exception; rather it may give you a garbage value.

Advertisement