Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / 编程 /Difference Between Definite Loop and Indefinite Loop

Difference Between Definite Loop and Indefinite Loop

June 15, 2011Posted byIndika

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循环也可以be used to implement definite loops as shown bellow (in Java).

int i=0;

while(i

{

//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.

Related posts:

Difference Between JDK and JRE Difference between Java and J2EE Difference Between Java and C language Difference Between Virtual and Abstract Difference Between Source Program and Object ProgramDifference Between Source Program and Object Program

Filed Under:编程Tagged With:definite loop,do-while loops,for loops,indefinite loop,iterations,Java,loop,while loop

About the Author:Indika

Indika, BSc.Eng, MSECE Computer Engineering, PhD. Computer Science, is an Assistant Professor and has research interests in the areas of Bioinformatics, Computational Biology, and Biomedical Natural Language Processing.

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Online Banking and e-Banking

What is the Difference Between RNA Viruses and Retroviruses

What is the Difference Between RNA Viruses and Retroviruses

Differences Between Fruits and Vegetables

Differences Between Fruits and Vegetables

Difference Between Hitler and Mussolini

Difference Between Hitler and Mussolini

Difference Between Alcoholic KOH and Aqueous KOH

Difference Between Alcoholic KOH and Aqueous KOH

Latest Posts

  • What is the Difference Between Sebum and Sweat
  • What is the Difference Between L Carnitine and Acetyl L Carnitine
  • What is the Difference Between Orthostatic Hypotension and Vasovagal Syncope
  • What is the Difference Between Astaxanthin and Zeaxanthin
  • What is the Difference Between Milia and Comedones
  • What is the Difference Between Phytoestrogens and Xenoestrogens
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Useand Privacy Policy:Legal.