Programmer's Wiki

For loop

409pages on
this wiki
Add New Page
Talk2 Share

Glossary: for loop - A loop construct used to count iterations, potentially to limit them.

Explanation Edit

To explain how a for loop works here is an example using a while loop

int i = 0;     // initialising the count to 0
while(i < 10){ // "i < 10" is the condition that allows the loop running
    print i;
    i++;       // increments counter by 1

So in this example the loop runs ten times.

  • the i variable is set to 0.
  • a condition is created for the loop to run if the "i < 10" test is successful.
  • the counter i is increased by 1 each time the loop runs

Here is the same example as a for loop:

for(int i = 0; i < 10; i++) {
    print i;

As you can see the three parts of the loop are all on the same line.

Notes Edit

  • For loops can start at any value, do not have to sequentially increment, may even run backwards with a negative value, and are not limited to whole integers.
  • Generally, modifying the index within the body of the loop is considered bad form and leads to bugs. The most common bug related to loops is the off-by-one (a.k.a. fence-post) error.
  • For loops do not need to work strictly with numerical values, for instance characters types may be used, and in some cases, enumerated values. In particular, with lower level languages, pointers to memory may be used; though incrementing a pointer often increases the actual value of the index by the size of a word.
  • The above examples are equivalent, but the transformation between them is not always legal. If the center of the loop contains a continue statement, then the incremental statement (i++) will run only if we are using the for version.

Code snippets Edit

Language: ActionScript 3 Edit

for ( var counter:int = 1; counter <= 5; counter++ )
    // Do something;

Language: BASIc Edit

FOR I = 1 TO 10
  REM Do something
FOR J = 1 TO 10 STEP 2

Language: Bash Edit

# first form
for i in {1..5}
    # must have at least one command in loop
    echo $i  # just print value of i
# second form
for (( i = 1; i <= 5; i++ ))
    # must have at least one command in loop
    echo $i  # just print value of i

Language: C Edit

int i;
for( i = 0; i < 10; i++ ) {
    /* Do something */

Language: C++/C#/ Edit

for( int i = 0; i < 10; i++ ) {
    // Do something 

C++ Code Examples

C++ allows for a variable to be defined within the scope of a looping construct, though that value is out of scope when the loop exits.

Language: Groovy Edit

Supports iterating over a range, array, or map with the in keyword:

for ( i in 0..20 ) {
    // Do something 

Groovy also supports the Java-style loops as well.

Language: Java Edit

for ( int i = 0; i < 20; i++ ) {
    // Do something
for ( int i : new int[]{1,2,3,4} ) {
    // do something

Language: JavaScript Edit

for( var i = 0; i < 10; i++ ) {
    // Do something

Language: Pascal Edit

for loop:=1 to 10 do
   {Do something}

In the above example, loop is an integer. The variable for the loop must be declared before its usage, but can be any ordinal type (included defined enumerated types). For example:

for loop:='a' to 'd' do
   {Do something}

Language: Perl Edit

 for( my $i=0; $i < 10; $i++ ) {
    # Do something

Language: Perl6 Edit

In Perl6 it is loop instead of for. See Perl6 Loop

loop( my Int $i = 0; $i < 10; $i++ ) {
  # Do something

Language: PHP Edit

for( $i = 0; $i < 10; $i++ ) {
    # Do something

Language: Python Edit

for i in range(10):
    # Do something

Language: Tcl Edit

for {set i 0} {$i < 10} {incr i} {
    # Do something

Language: IDL Edit

for i=0L, 10-1 do begin 
    ; Do something

Language: Visual Basic Edit

For i As Integer = 0 To 9
    ' Do something
for count= 1-10


end for

Reduction Edit

Looping constructs can almost always (see notes) be reduced to the following pseudo code:

while ( some_condition_is_true ) {

Lua Edit

Lua offers the for loop in two forms. One is intended for counting:

-- Say hello world ten times
for i = 1, 10 do
    print("Hello World!");

The second available form calls a function upon each iteration, and will continue as long as the function's return is not nil. By using a closure, a function can maintain state between successive calls to traverse data.

To iterate through a list, there are one of two allowed syntaxes:

-- print out a list
-- i contains the index, while v contains the value.
local t = {3,4,5,6,7,2,5}
for i,v in ipairs(t) do

The alternate method:

-- print out a list
-- k contains the index of each value. 
local t = {3,4,5,6,7,2,5}
for k in pairs(t) do

See Also Edit

Control structures
If statement - Switch statement - While loop - For loop - Foreach loop - Do while loop

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.