For Loop

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 
In Java:
       int factorial = 1;
for (int i = 1; i <= 5; i++) {
factorial = factorial * i;
}
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

 

<< PreviousWord BrowserNext >>
dietrich fischer dieskau
nicholas rowe (dramatist)
governorates of egypt
white day
thomas betterton
noel lynch
carolyn watkinson
anne oldfield
shuifu
list of curves
susanna verbruggen
jessica barone
mozilla composer
william mountfort
fisher equation
geometric dimensioning and tolerancing
nathan drake
patrick bateman
margaret wright
glen iris, victoria
aquiles serdn
georg gottfried gervinus
principal speaker
while loop
frank r. pierson
james burbage
blaise de vigenre
bensonhurst
city loop, melbourne
ibrahim al hamadi
novatianism
holiday cactus
uss richard e. byrd (ddg 23)
metcard
exponential sum
john beaumont
1676 in science
fujia yang
lynne featherstone
george sandys
william chalmers
latin names of european communities
victoria ruffo
sally hamwee