Long Live Guarded Commands

Nara Bagi
2 min readNov 8, 2020

The definition of a guarded command speaks for itself — a command that is “guarded”. The term was introduced by Dijkstra back in the 1970s and, despite its importance, occupies only a small section in Sebesta’s “Concepts of Programming Languages”. So, what’s so special about guarded commands?

Guarded commands are a special kind of control structures with two forms:

  • Guarded If-Statements, and
  • Guarded Do-Loops.

According to Dijkstra, guarded commands have the following representation:

Application of a Guarded Command

Each line in the code above is a guarded command: the Boolean expressions are “guarding” the statements next to them.

Guarded commands go beyond a simple if statement in that they are non-deterministic: if the guards are not disjoint, more than one expression can evaluate to True at the same time — hence, in the case of selection, the program is not well-defined and places an emphasis on the development of a more reliable code. If such a scenario takes place, one implementation can choose to execute the first expression that evaluates to True, while another — the last True expression.

Guarded Selection has the following structure:

Guarded Selection Example

It resembles the famous case control structure, with the only difference in that case is deterministic because its guards are disjoint sets.

If none of the Boolean expressions evaluates to True, then the program is simply aborted.

Guarded Repetition has the following structure:

Guarded Repetition Example

If more than one expression is True, one of the statements is randomly chosen for execution and the looping continues until all guards evaluate to False.

In essence, for a programmer, a guarded command is nothing more than a very comprehensive conditional statement that encourages a more systematic program development and verifies program correctness.

Dijkstra also proposed this term as an alternative selection and looping control structure that introduces non-determinism to programs and can help handle concurrency. In parallel architectures, all guards are evaluated at once and the first (depending on implementation) statement whose guard is True is executed.

References:

Sebesta, R. W. (2016). Concepts of Programming Languages, 11th edition. Chapter 8, pp.380–382

Waggener, J. L. Guarded Command. Retrieved from https://dl.acm.org/doi/pdf/10.5555/1074100.1074433#:~:text=The%20term%20guarded%20command%2C%20as,execution%20is%20controlled%20by%20B.

--

--