Loop

From Conservapedia

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
  PRINT I
NEXT I

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();
  box.shipToCustomer();

  shipment.remove(box);

}

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 {

  queue.printStatus();
  queue.processFirstItem();

} while (queue.hasItems());

Categories: [Computer Programming]


Download as ZWI file | Last modified: 02/24/2023 16:43:18 | 22 views
☰ Source: https://www.conservapedia.com/Loop | License: CC BY-SA 3.0

ZWI signed:
  Encycloreader by the Knowledge Standards Foundation (KSF) ✓[what is this?]