# Loops in Prolog

The looping facility is contained in most of the programming languages. Looping is used to enable a set of instructions to be repeatedly executed either a fixed number of times or until a given condition met. Prolog has no looping facility, but we can obtain a similar effect. Using this effect, we can evaluate a sequence of goals repeatedly. In various ways, this can be done like built-in predicates, recursion, backtracking, or a combination of these.

### Looping a fixed number of times

To execute the instruction a fixed number of times, many programming languages provide 'for loop'. In Prolog, there is no such facility available directly, but using recursion, we can obtain a similar effect, which is shown in the following programs:

Example 1:

This program outputs the integer value from a specified value down to 1.

In the above example, we define the loop predicate in terms of itself. The second clause read as 'to loop from N, first write the N's value, then subtract one to provide S, then loop from M'. This process will terminate using the first clause: 'when the argument is 0, then stop or do nothing'. Here the first clause is called as terminated condition.

In the second clause, we are using two goals S is N-1, loop(S) for the loop predicate. Prolog will not work on the obvious alternative of the loop(N-1). When evaluating goals with is functor, or with relational operators, only then Prolog evaluates expressions like N-1. If we are using N-1 as an argument of the predicate, it will mean that the term with arguments N and 1, and with infix operator -(minus sign).

Example 2:

The following program generates an integer value from first to last inclusive.

The above output_value has two arguments. It can be read as 'output the integers from First to Last inclusive'. The above loop terminates when the value of both arguments are same.

Example 3:

In this program, we will define a predicate that can find the sum of integers from 1 to N. Let's assume N=100.

For this, we start with 1, then add 2, then add 3, ….., then add 100. If we re-expressed this process declaratively in terms of itself, the process will be much easier to program.

100 plus the sum of the first 99 integers is the sum of the first 100 integers.

99 plus the sum of the first 98 integers is the sum of the first 99 integers.

98 plus the sum of the first 97 integers is the sum of the first 98 integers.

…………………………………………………………………………….

3 plus the sum of the first 2 integers is the sum of the first 3 integers.

2 plus the sum of the first 1 integer is the sum of the first 2 integers.

1 is the sum of the first one integer.

To consider this process, we have two distinct cases, the general case and the terminating case. In general case: 'the sum of first N integers is the sum of first N-1 integers, plus N'. In terminating case: 'the sum of first 1 integer is 1'.

For holding the value of N-1, it is essential to use the additional N1 variable. Writing sumto(N-1, M1), etc. instead would work not work correctly. N-1 is not a numerical value, but it is a term.

Example 4:

This program is used to read the first 6 terms from the given file and then write these terms to the current output stream. Just like Example 1, it uses a method 'counting down'.