0

I wonder when idx is -1 why this causes the runtime exception. It says the array cannot access cause idx is -1. But in my thought, idx -1 doesn't match the first condition (idx >= 0) so it should skip the next condition (open[idx] ,,,)

    if ((idx >= 0) && (open[idx] == '(') || (open[idx] == '{')) {

However, this code pass

    if ((idx >= 0) && ((open[idx] == '(') || (open[idx] == '{'))) {

I cannot understand why two conditional statements work differently. Please help me explain. Thanks

  • 3
    Parentheses work just like in math - they group operations to override operator precedence. – SuperStormer Sep 04 '21 at 04:37
  • in the second case, the other checks are not being evaluated because of Short-Circuiting, this is why you don't get an exception. It's not purely the evaluation order, but you also need to understand how short circuiting works – felipeek Sep 04 '21 at 04:45
  • I got it. It's like A && B || C In this case, it fails because C. Thanks! – songsong Sep 04 '21 at 05:17

0 Answers0