|
For - Definition and Overview |
|
|
|
|
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
- While loop
- Do while loop
- Loop counter
|
|
Example Usage of For |
 |
KVSarmaJ: @Alamsha_Karnan Stop talks with Pak until all the terrorist activities in J&K are stopped.Stop looking For help. Nobody will!To mention some |
 |
songkick: @anjali28 Hey Anjali - both issues are sorted now. We're always working on improving our data quality, so thanks For the tip-off! |
 |
aarondotcomm: Instead of going about your business as usual, it's more excit... More For Taurus http://twittascope.com/twittascope/?sign=2 |
|