Guarded Command Language
Overview
Guarded commands are special set of conditional and loop commands. They were founded by Dijikstra, within needs of deterministic mechanism. In simpliest form, there are two forms (commands) : conditional command and loop command.
Notation:
C - Condition
S - Statement
Conditional command IF
Syntax of command:
if [command1] [command2] ... fi
Where [command] has syntax:
C S
The Statement part is executed ONLY if Condition is true. Moreover, if more than one Statement of guarded command is true, there is choosen only one (randomly or nondeterministic). State without true condition causes runtime error.
Example:
Classic expression in common languages f.ex.:
if a b then print "More or equal"; else if a < b then print "Less";
Can be rewritten to:
if a b print "More or equal" a < b print "Less" fi
The power of guarded command is in following expression:
if a b print "More or equal" a b print "Less or equal" fi
When a == b, the result of command can be one "More or equal" or "Less or equal".
Loop command DO
Syntax:
do [command1] [command2] ... od
Loop will be executed until one or more Conditions are true. If more than one Condition is true, one Statement will be choosen.
Example:
Program for Greater Common Divider (GCD) :
Variant 1:
x,y = X,Y do x y if x > y x := x-y y > x x := y-x fi od print (x)
Variant 2:
x,y = X,Y do x > y x := x-y y > x x := y-x od print (x)