HANDBOOK / GENTLE PRIMER / Control Structures /

# Using Alternative and Conditional Statements

Alternative and conditional statements allow decisions within a rule body without the necessity of introducing an auxiliary predicate. When should predicates be used?

Of course predicates are necessary if the computation is recursive. Predicates should also be used when the same piece of code occurs more than once.

In addition, a predicate should be used to name a subcomputation if it is otherwise hard to understand or if the rule body gets longer than one can grasp. With conditional statements, there is a the danger of writing long rule bodies that are hard to understand.

On the other hand, a short conditional, given directly, is often more expressive than a newly invented name for an auxiliary predicate.

Conditional statements should also be used when correctness relies on the fact that earlier alternatives did not succeed (shallow backtracking). While rules are best understood if one can read them in isolation, conditional statements are read in an if-then-else style.