For_loop For_loop

For loop - Definition and Overview

Related Words: Annulus, Arc, Arch, Areola, Association, Aureole, Band, Bank, Beat, Belt, Bend, Beset, Bilge, Blain, Blister, Blob, Blowhole, Boss, Bow

In most imperative computer programming languages, a for loop is a control structure which allows code to be executed iteratively. For loops, unlike while loops, are typically used when the number of iterations are known before entering the loop.

Examples

These for loops will calculate the factorial of a number:

In QBasic or Visual Basic:

	Dim Factorial as Long
Factorial = 1
For Counter = 5 to 1 Step -1 Factorial = Factorial * Counter
Next
Print Factorial

In C++:

 	int factorial = 1;
for (int i = 1; i <= 5; i++)
factorial *= i;
std::cout << factorial;

In Python:

     f = 1
     for i in xrange(1, 10):
         f *= i
     print f

A for loop can always be converted into a while loop.

See also

  1. While loop
  2. Do while loop
  3. Loop counter

Copyright 2009 WordIQ.com - Privacy Policy  :: Terms of Use  :: Contact Us  :: About Us
This article is licensed under the GNU Free Documentation License. It uses material from the this Wikipedia article.