Compare the Difference Between Similar Terms

Difference Between Definite Loop and Indefinite Loop

Definite Loop vs Indefinite Loop

A loop is a block of code that would repeat for a specified number of times or until some condition is satisfied. A definite loop is a loop in which the number of times it is going to execute is known in advance before entering the loop. In an indefinite loop, the number of times it is going to execute is not known in advance and it is going to be executed until some condition is satisfied.

What is a Definite Loop?

A definite loop is a loop in which the number of times it is going to execute is known in advance before entering the loop. The number of iterations it is going to repeat will be typically provided through an integer variable. In general, for loops are considered to be definite loops. Following is an example of a definite loop implemented using a for loop (in Java programming language).

for (int i = 0; i < num; i++)

{

//body of the for loop

}

The above loop will execute its body a number of times provided by the num variable. This could be determined from the initial value of variable i and the loop condition.

While loops can also be used to implement definite loops as shown bellow (in Java).

int i=0;

while(i<num)

{

//body of the loop

i++;

}

Even though this uses a while loop, this is also a definite loop, since it is known in advance that the loop is going to execute number of times provided by the num variable.

What is an Indefinite Loop?

In an indefinite loop, the number of times it is going to execute is not known in advance. Typically, an indefinite loop is going to be executed until some condition is satisfied. While loops and do-while loops are commonly used to implement indefinite loops. Even though there is no specific reason for not using for loops for constructing indefinite loops, indefinite loops could be organized neatly using while loops. Some of common examples that you would need to implement indefinite loops are prompting for reading an input until user inserts a positive integer, reading a password until the user inserts the same password twice in a row, etc.

What is the difference between Definite Loop and Indefinite Loop?

A definite loop is a loop in which the number of times it is going to execute is known in advance before entering the loop, while an indefinite loop is executed until some condition is satisfied and the number of times it is going to execute is not known in advance. Often, definite loops are implemented using for loops and indefinite loops are implemented using while loops and do-while loops. But there is no theoretical reason for not using for loops for indefinite loops and while loops for definite loops. But indefinite loops could be neatly organized with while loops, while definite loops could be neatly organized with for loops.