# Short-circuit evaluation in if?

**URL:** <https://discourse.cmake.org/t/short-circuit-evaluation-in-if/1608>\
**Category:** Code\
**Created:** [July 22, 2020, 2:01pm UTC](https://discourse.cmake.org/t/short-circuit-evaluation-in-if/1608 "2020-07-22T14:01:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leherenn](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/ba9def/32.png) [@Leherenn](https://discourse.cmake.org/u/Leherenn)\
**Post date:** [July 22, 2020, 2:01pm UTC](https://discourse.cmake.org/t/short-circuit-evaluation-in-if/1608/1 "2020-07-22T14:01:45Z")

</div>

I could not find it in the documentation ([https://cmake.org/cmake/help/latest/command/if.html](https://cmake.org/cmake/help/latest/command/if.html)), but it would seem that conditions do not short-circuit.  
Is that correct?

`if(DEFINED MY_VAR AND ${MY_VAR} STREQUAL "true")`  
The command above returns:

```
CMake Error at CMakeLists.txt:23 (if):
  if given arguments:

    "DEFINED" "MY_VAR" "AND" "STREQUAL" "true"

  Unknown arguments specified

```

which leads to think the second part is evaluated, and returns nothing (and not an empty string), when the variable is not defined.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [July 22, 2020, 2:11pm UTC](https://discourse.cmake.org/t/short-circuit-evaluation-in-if/1608/2 "2020-07-22T14:11:07Z")

</div>

I think it first perform all the required substitutions and then parses the command. This gives you the possibility to define entire condition in a variable. Then are the arguments checked and it blows up.

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [July 22, 2020, 2:14pm UTC](https://discourse.cmake.org/t/short-circuit-evaluation-in-if/1608/3 "2020-07-22T14:14:40Z")

</div>

You can simply write (i.e. do not expand the variable):

```cmake
if (MY_VAR STREQUAL "true")

```

In this case, a non-existing variable will evaluate to `FALSE` (boolean value as defined by `CMake`).

And, in the case of expected value is a `true` value as defined by `CMake`:

```auto
if (MY_VAR)

```

---

<div class="post-metadata">

**Author:** ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)\
**Post date:** [July 22, 2020, 3:15pm UTC](https://discourse.cmake.org/t/short-circuit-evaluation-in-if/1608/4 "2020-07-22T15:15:59Z")

</div>

To expand on the accepted answer, It’s not short circuit behavior.

Refer to [https://cmake.org/cmake/help/v3.18/command/if.html](https://cmake.org/cmake/help/v3.18/command/if.html) and Variable Expansion.

> The if command was written very early in CMake’s history, predating the `${}` variable evaluation syntax, and for convenience evaluates variables named by its arguments as shown in the above signatures. Note that normal variable evaluation with `${}` applies before the if command even receives the arguments.
