简体   繁体   English

评估&&布尔运算符

[英]Evaluation of && boolean operator

If i have the following if statement 如果我有以下if声明

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

Where row is an int value and array is an int[][] object. 其中rowint值, arrayint[][]对象。

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? 我的问题是,如果row = -1会抛出异常,因为数组不会有-1字段,那么out of bounds异常? Or will it stop at the first part of the if, the (row!=-1) and because that is false, it will ignore the rest? 或者它会停在if的第一部分(行!= - 1),因为这是假的,它会忽略其余部分吗? Or to be sure it doesn't throw exception, i should separate the above if statement into two? 或者为了确保它不会抛出异常,我应该将上面的if语句分成两个?

(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 ...) (请不要告诉我为自己检查这个:)我在这里问'因为我想问一个后续问题......)

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). &&是一个短路布尔运算符 ,这意味着只要一个部分返回false就会停止执行表达式(因为这意味着整个表达式必须为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. 请注意,它还保证按顺序评估表达式的各个部分,因此在这些情况下使用是安全的。

It will not throw an exception. 它不会抛出异常。 However, if row is < -1 (-2 for example), then you're going to run into problems. 但是,如果row <-1(例如-2),那么您将遇到问题。

It will stop at the first part of the if. 它会停在if的第一部分。 Java uses short circuite evaluation . Java使用短路评估

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... 如果第一个表达式为false,编译器将不会检查第二个表达式...这就是为什么&&被称为“短路”运算符...

通过&&调用短路评估 ,如果行检查失败,则继续评估没有意义。

Most programming languages short-circuit the test when the first expression returns false for an AND test and true for an OR test. 大多数编程语言短路时,第一个表达式返回试验false为测试和true一个或测试。 In your case, the AND test will be short-circuited and no exception will occur. 在您的情况下,AND测试将被短路并且不会发生异常。

Many programming languages have short-circuit evaluation for logical operators. 许多编程语言都对逻辑运算符进行短路评估

In a statement such as A and B , the language will evaluate A first. 在诸如A and B的语句中,语言将首先评估A If A is false, then the entire expression is false; 如果A为假,则整个表达式为假; it doesn't matter whether B is true or false. B是真还是假都没关系。

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. 在你的情况下,当row等于-1row != -1将为false,并且不会评估数组表达式的短路。

Also, your second question about the behavior of the array index is entirely language-dependent. 此外,关于数组索引行为的第二个问题完全取决于语言。 In C, array[n] means *(array + n) . 在C中, array[n]表示*(array + n) In python, array[-1] gives you the last item in the array. 在python中, array[-1]为您提供array[-1]的最后一项。 In C++, you might have an array with an overloaded [] operator that accepts negative indexes as well. 在C ++中,您可能有一个带有重载[]运算符的数组,该运算符也接受负索引。 In Java, you'll get an ArrayIndexOutOfBoundsException . 在Java中,您将获得ArrayIndexOutOfBoundsException

Also, you might need something like the following (or just use a try/catch). 此外,您可能需要以下内容(或使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM