$= Additional control structures Despite the declarative semantics of logic programming, Prolog programmers often rely on control structures that are reminiscent of control structures encountered in traditional imperative languages. Iteration Backtracking may be used to perform an operation iteratively. For instance, if we define the predicate natural by the clauses \pprog{natural}{ .ds natural.pl } then the goal ?- natural(X), write(X), X = 100. results in writing the first 101 natural numbers, including zero, to the screen. The iteration thus implemented is often referred to as a failure-driven loop and is in some cases a viable and efficient alternative to the use of recursion. Conditional branching Just as we implemented the (meta) predicate not, we may implement a (non-logical) conditional operator. \prologindex{\condexpr} \pprog{cond}{ A -> B :- A, !, B. } The clause defining the implication arrow enforces that the condition A is only evaluated once. Likewise we may implement a disjunctive operator by the clauses \prologindex{\orexpr} \pprog{or}{ A ; B :- A. A ; B :- B. } These clauses result in evaluating the first component of the disjunction and, only if the need arises, on backtracking the second component, as an alternative solution to the disjunctive goal. \prologindex{\condbranch} Combined the conditional operator and the disjunctive operator may be used as in A->B;C\ to express the conditional {\em if A then B else C }.
A ; B :- A. A ; B :- B.