When using logical && condition in some special cases like cond1 && cond2, it seems that it is inconsistent with the so called “short-circuit evaluation“. In FreeFem++, both conditions cond1 and cond2 are checked, while in C/C++, if the first condition cond1 is false, then the 2nd condition cond2 may not be checked.
This problem occurs in an example which finds the unique elements in an array.
The logical && case is as follows:
int[int] a = [0, 1, 1, 2, 2, 3];
for (int j = 0; j < a.n; j++) {
if (j > 0 && a(j) == a(j-1)) {
continue;
}
cout << a(j) << endl;
}
Here, when j == 0, the condition j > 0 is false, so (j > 0 && a(j) == a(j-1)) is also false. But, the 2nd condition a(j) == a(j-1) is still checked in FreeFem++, which meets a error as:
Out of bound 0 <=-1 < 6 array type = P2KNIlE
Although this can be addressed by dividing these conditions into two if statements as follows:
int[int] a = [0, 1, 1, 2, 2, 3];
for (int j = 0; j < a.n; j++) {
if (j > 0) {
if (a(j) == a(j-1)) {
continue;
}
}
cout << a(j) << endl;
}
Is it possible to use the “short-circuit evaluation“ directly in the logical “and“ statement? My FreeFem++ version is 4.14.