A typical example is checking that a reference about to be processed is not null, which avoids null-pointer failures.
In general, less nesting is good, as it simplifies the code and reduces cognitive burden.
For example, in Python: Another example, written in C: The term is used with specific meaning in APL, Haskell, Clean, Erlang, occam, Promela, OCaml, Swift,[2] Python from version 3.10, and Scala programming languages.
In the following Haskell example, the guards occur between each pair of "|" and "=": This is similar to the respective mathematical notation:
Some common ways to write this: If the second sub-expression can be a further simple conditional expression, we can give more alternatives to try before the last fall-through: In 1966 ISWIM had a form of conditional expression without an obligatory fall-through case, thus separating guard from the concept of choosing either-or.
KRC, a "miniaturized version"[3] of SASL (1976), was one of the first programming languages to use the term "guard".
In 1996, Dyalog APL adopted an alternative pure functional style in which the guard is the only control structure.