AndreiBogdan
AndreiBogdan

Reputation: 11164

Evaluation of && boolean operator

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 ...)

Upvotes: 5

Views: 1471

Answers (8)

Java42
Java42

Reputation: 7706

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;
}

Upvotes: 1

Seth
Seth

Reputation: 46433

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.

Upvotes: 1

torrential coding
torrential coding

Reputation: 1765

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.

Upvotes: 1

J&#233; Queue
J&#233; Queue

Reputation: 10637

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

Upvotes: 1

Amit
Amit

Reputation: 13364

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...

Upvotes: 1

mikera
mikera

Reputation: 106351

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.

Upvotes: 3

mroselli
mroselli

Reputation: 151

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

Upvotes: 2

MByD
MByD

Reputation: 137362

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

Upvotes: 1

Related Questions