<s>
Regardless	O
of	O
which	O
programming	O
language	O
is	O
used	O
,	O
a	O
guard	B-Language
clause	I-Language
,	O
guard	O
code	O
,	O
or	O
guard	B-Language
statement	I-Language
,	O
is	O
a	O
check	O
of	O
integrity	O
preconditions	O
used	O
to	O
avoid	O
errors	O
during	O
execution	O
.	O
</s>
<s>
Other	O
uses	O
include	O
using	O
a	O
boolean	O
field	O
for	O
idempotence	O
(	O
so	O
subsequent	O
calls	O
are	O
nops	O
)	O
,	O
as	O
in	O
the	O
dispose	B-Application
pattern	I-Application
.	O
</s>
<s>
The	O
guard	O
provides	O
an	O
early	O
exit	O
from	O
a	O
subroutine	O
,	O
and	O
is	O
a	O
commonly	O
used	O
deviation	O
from	O
structured	B-Language
programming	I-Language
,	O
removing	O
one	O
level	O
of	O
nesting	O
and	O
resulting	O
in	O
flatter	O
code	O
:	O
replacing	O
if	O
guard	O
{	O
...	O
}	O
with	O
if	O
not	O
guard	O
:	O
return	O
;	O
....	O
</s>
<s>
Using	O
guard	B-Language
clauses	I-Language
can	O
be	O
a	O
refactoring	O
technique	O
to	O
improve	O
code	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
Python	B-Language
:	O
</s>
<s>
The	O
term	O
is	O
used	O
with	O
specific	O
meaning	O
in	O
APL	B-Language
,	O
Haskell	B-Language
,	O
Clean	B-Operating_System
,	O
Erlang	B-Operating_System
,	O
occam	B-Language
,	O
Promela	B-Application
,	O
OCaml	B-Language
,	O
Swift	B-Application
,	O
Python	B-Language
from	O
version	O
3.10	O
,	O
and	O
Scala	B-Application
programming	I-Application
languages	I-Application
.	O
</s>
<s>
In	O
Mathematica	B-Language
,	O
guards	O
are	O
called	O
constraints	O
.	O
</s>
<s>
Guards	O
are	O
the	O
fundamental	O
concept	O
in	O
Guarded	B-Application
Command	I-Application
Language	I-Application
,	O
a	O
language	O
in	O
formal	B-Architecture
methods	I-Architecture
.	O
</s>
<s>
Guards	O
can	O
be	O
used	O
to	O
augment	O
pattern	B-Language
matching	I-Language
with	O
the	O
possibility	O
to	O
skip	O
a	O
pattern	O
even	O
if	O
the	O
structure	O
matches	O
.	O
</s>
<s>
Boolean	O
expressions	O
in	O
conditional	B-Language
statements	O
usually	O
also	O
fit	O
this	O
definition	O
of	O
a	O
guard	O
although	O
they	O
are	O
called	O
conditions	O
.	O
</s>
<s>
In	O
the	O
following	O
Haskell	B-Language
example	O
,	O
the	O
guards	O
occur	O
between	O
each	O
pair	O
of	O
"	O
|	O
"	O
and	O
"	O
=	O
"	O
:	O
</s>
<s>
However	O
,	O
in	O
Haskell	B-Language
list	B-Language
comprehensions	I-Language
the	O
guards	O
are	O
in	O
series	O
,	O
and	O
if	O
any	O
of	O
them	O
fails	O
,	O
the	O
list	O
element	O
is	O
not	O
produced	O
.	O
</s>
<s>
This	O
would	O
be	O
the	O
same	O
as	O
combining	O
the	O
separate	O
guards	O
with	O
logical	O
AND	O
,	O
except	O
that	O
there	O
can	O
be	O
other	O
list	B-Language
comprehension	I-Language
clauses	O
among	O
the	O
guards	O
.	O
</s>
<s>
A	O
simple	O
conditional	B-Language
expression	O
,	O
already	O
present	O
in	O
CPL	B-Language
in	O
1963	O
,	O
has	O
a	O
guard	O
on	O
first	O
sub-expression	O
,	O
and	O
another	O
sub-expression	O
to	O
use	O
in	O
case	O
the	O
first	O
one	O
cannot	O
be	O
used	O
.	O
</s>
<s>
If	O
the	O
second	O
sub-expression	O
can	O
be	O
a	O
further	O
simple	O
conditional	B-Language
expression	O
,	O
we	O
can	O
give	O
more	O
alternatives	O
to	O
try	O
before	O
the	O
last	O
fall-through	O
:	O
</s>
<s>
In	O
1966	O
ISWIM	B-Language
had	O
a	O
form	O
of	O
conditional	B-Language
expression	O
without	O
an	O
obligatory	O
fall-through	O
case	O
,	O
thus	O
separating	O
guard	O
from	O
the	O
concept	O
of	O
choosing	O
either-or	O
.	O
</s>
<s>
In	O
the	O
case	O
of	O
ISWIM	B-Language
,	O
if	O
none	O
of	O
the	O
alternatives	O
could	O
be	O
used	O
,	O
the	O
value	O
was	O
to	O
be	O
undefined	O
,	O
which	O
was	O
defined	O
to	O
never	O
compute	O
into	O
a	O
value	O
.	O
</s>
<s>
KRC	B-Language
,	O
a	O
"	O
miniaturized	O
version	O
"	O
of	O
SASL	B-Language
(	O
1976	O
)	O
,	O
was	O
one	O
of	O
the	O
first	O
programming	O
languages	O
to	O
use	O
the	O
term	O
"	O
guard	O
"	O
.	O
</s>
<s>
Use	O
of	O
guard	B-Language
clauses	I-Language
,	O
and	O
the	O
term	O
"	O
guard	B-Language
clause	I-Language
"	O
,	O
dates	O
at	O
least	O
to	O
Smalltalk	B-Application
practice	O
in	O
the	O
1990s	O
,	O
as	O
codified	O
by	O
Kent	O
Beck	O
.	O
</s>
<s>
In	O
1996	O
,	O
Dyalog	O
APL	B-Language
adopted	O
an	O
alternative	O
pure	O
functional	O
style	O
in	O
which	O
the	O
guard	O
is	O
the	O
only	O
control	O
structure	O
.	O
</s>
<s>
This	O
example	O
,	O
in	O
APL	B-Language
,	O
computes	O
the	O
parity	O
of	O
the	O
input	O
number	O
:	O
</s>
<s>
In	O
addition	O
to	O
a	O
guard	O
attached	O
to	O
a	O
pattern	O
,	O
pattern	O
guard	O
can	O
refer	O
to	O
the	O
use	O
of	O
pattern	B-Language
matching	I-Language
in	O
the	O
context	O
of	O
a	O
guard	O
.	O
</s>
<s>
This	O
meaning	O
was	O
introduced	O
in	O
a	O
proposal	O
for	O
Haskell	B-Language
by	O
Simon	O
Peyton	O
Jones	O
titled	O
in	O
April	O
1997	O
and	O
was	O
used	O
in	O
the	O
implementation	O
of	O
the	O
proposal	O
.	O
</s>
<s>
An	O
example	O
in	O
extended	O
Haskell	B-Language
:	O
</s>
<s>
...	O
"	O
As	O
in	O
list	B-Language
comprehensions	I-Language
,	O
the	O
guards	O
are	O
in	O
series	O
,	O
and	O
if	O
any	O
of	O
them	O
fails	O
the	O
branch	O
is	O
not	O
taken	O
.	O
</s>
