# Logical "and" condition inconsistent with the "short-circuit evaluation"?

**URL:** https://community.freefem.org/t/logical-and-condition-inconsistent-with-the-short-circuit-evaluation/4120
**Category:** General Discussion
**Created:** [November 2, 2025, 8:43am UTC](https://community.freefem.org/t/logical-and-condition-inconsistent-with-the-short-circuit-evaluation/4120 "2025-11-02T08:43:49Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![MatrixVector](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@MatrixVector](https://community.freefem.org/u/MatrixVector)
#### Post date: [November 2, 2025, 8:43am UTC](https://community.freefem.org/t/logical-and-condition-inconsistent-with-the-short-circuit-evaluation/4120/1 "2025-11-02T08:43:49Z")

</div>

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:

```cpp
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:

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

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [November 2, 2025, 1:24pm UTC](https://community.freefem.org/t/logical-and-condition-inconsistent-with-the-short-circuit-evaluation/4120/2 "2025-11-02T13:24:59Z")

</div>

No, it’s not possible, and you are right that the behavior is different than in plain C++.
