5

If i have the following if statement

if ( (row != -1) && (array[row][col] != 10) ) {
    ....
}

Where row is an int value and array is an int[][] object.

My question is, if this will throw an exception if row = -1 as the array won't have a -1 field, so out of bounds exception? Or will it stop at the first part of the if, the (row!=-1) and because that is false, it will ignore the rest? Or to be sure it doesn't throw exception, i should separate the above if statement into two?

(Pls, don't tell me to check this out for my self :) I'm asking here 'cause i wanna ask a followup question as well ...)

Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53
AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106
  • 1
    it will stop on the first condition if it evaluates to false – Asaf Mar 13 '12 at 05:44
  • 1
    Java is one of the programming languages that has short-circuiting for logical operators, so it works as you'd expect. VB and SQL are languages that do not have this feature, meaning you would have to use a different means to avoid an out-of-bounds exception. – Gabe Mar 13 '12 at 05:46
  • 1
    It will stop at the 1st test and not try the 2nd test. – Java42 Mar 13 '12 at 05:46

8 Answers8

3

It will stop safely before throwing an exception

The && is a short-circuiting boolean operator, which means that it will stop execution of the expression as soon as one part returns false (since this means that the entire expression must be false).

Note that it also guaranteed to evaluate the parts of the expression in order, so it is safe to use in situations such as these.

mikera
  • 105,238
  • 25
  • 256
  • 415
  • And i'm guessing if i have multiple statements like the above, separated with an OR operator ||, it still won't throw exception ... as each && will individually short-cirtuit? what i mean is if ( (&&) || (&&) ) – AndreiBogdan Mar 13 '12 at 05:50
  • 1
    Correct.... as long as each sub-statement short circuits successfully then you will be fine. – mikera Mar 13 '12 at 05:52
2

It will not throw an exception. However, if row is < -1 (-2 for example), then you're going to run into problems.

mroselli
  • 151
  • 5
1

It will stop at the first part of the if. Java uses short circuite evaluation.

MByD
  • 135,866
  • 28
  • 264
  • 277
1

No, It wont. the compiler will not check the second expression if the first expression is false... That is why && is called "short circuit" operator...

Amit
  • 13,134
  • 17
  • 77
  • 148
1

Called a short-circuit evaluation via the && and if the row check fails, there is no point in continuing evaluation.

Jé Queue
  • 10,359
  • 13
  • 53
  • 61
1

Most programming languages short-circuit the test when the first expression returns false for an AND test and true for an OR test. In your case, the AND test will be short-circuited and no exception will occur.

torrential coding
  • 1,755
  • 2
  • 24
  • 34
1

Many programming languages have short-circuit evaluation for logical operators.

In a statement such as A and B, the language will evaluate A first. If A is false, then the entire expression is false; it doesn't matter whether B is true or false.

In your case, when row is equal to -1, row != -1 will be false, and the short-circui the array expression won't be evaluated.

Also, your second question about the behavior of the array index is entirely language-dependent. In C, array[n] means *(array + n). In python, array[-1] gives you the last item in the array. In C++, you might have an array with an overloaded [] operator that accepts negative indexes as well. In Java, you'll get an ArrayIndexOutOfBoundsException.

Community
  • 1
  • 1
Seth
  • 45,033
  • 10
  • 85
  • 120
1

Also, you might need something like the following (or just use a try/catch).

boolean isItSafe(int[][] a, int x, int y) {
    boolean isSafe = true;
    if (a == null || a.length == 0 || x >= a.length || x < 0 || y < 0 || y >= a[0].length ) {
        isSafe = false;
    }
    return isSafe;
}
Java42
  • 7,628
  • 1
  • 32
  • 50