From Conservapedia
Jump to: navigation, search

A loop in computer programming is a section of instructions which are repeated - either endlessly or until a certain expected state occurs. A common error made by novice programmers is to program an infinite loop, i.e., one that never ends despite the programmer's hopes.

Fortran introduced the do loop, which BASIC made into the for loop. A counter is given an initial value. If it is below the maximum value, the loop's instructions are executed. Then the counter is set to the next value.

This trivial example prints out the first five integers:

FOR I = 1 TO 5

C, Java and the other "curly brace" languages put all the control code on the first line. They typically start counting at zero, which is the first element in most arrays:

for (int i = 0; i < 5; i++) {

  System.out.println( employee(i) );


A while loop doesn't need a counter:

while ( shipment.hasItems() > 0 ) {

  Item box = shipment.getFirst();



Although a while loop won't run at all unless the condition is true, the do while loop is guaranteed to run at least once. It tests the condition at the end of each execution:

do {


} while (queue.hasItems());