Advanced | Help | Encyclopedia
Directory


For loop

(Redirected from For)

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 is known before entering the loop.

Table of contents

Examples

These for loops will calculate the factorial of a number:

QBasic or Visual Basic

 Dim factorial As Long : factorial = 1
 Dim counter As Integer
 For counter = 1 To 5
   factorial = factorial * counter
 Next
 Print factorial

C or C++

 unsigned long factorial = 1;
 for (unsigned int counter = 1; counter <= 5; counter++)
   factorial *= counter;
 printf("%i", factorial);

Python

 factorial = 1
 for counter in xrange(1, 5):
   factorial *= counter
 print factorial

Java

 long factorial = 1;
 for (int counter = 1; counter <= 5; counter++)
   factorial *= counter;
 System.out.println(factorial);

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

See also

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







Links: Addme | Keyword Research | Paid Inclusion | Femail | Software | Completive Intelligence

Add URL | About Slider | FREE Slider Toolbar - Simply Amazing
Copyright © 2000-2008 Slider.com. All rights reserved.
Content is distributed under the GNU Free Documentation License.