Wikia

Programmer's Wiki

For loop

Talk2
393pages on
this wiki

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
NEXT I
 
FOR J = 1 TO 10 STEP 2
  PRINT J
NEXT J
END

Language: Bash Edit

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

Language: C Edit

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

Language: C++/C#/Vala Edit

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

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
}
<html> 

<head><title>FOR LOOP Example</title></head> <body> <%for ( fontSize = 1; fontSize <= 3; fontSize++){ %> TUTORIALS POINT Simply Easy Learning     <font color="green" size="<%= fontSize %>">  JSP Tutorial  </font>
 <%}%> </body> </html>

Language: JavaScript Edit

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

Language: Pascal Edit

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

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
begin
   {Do something}
end;

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
endfor

Language: Visual Basic Edit

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

addition

end for

Reduction Edit

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

initialize_index
while ( some_condition_is_true ) {
  do_body_of_loop
  modify_index
}

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!");
end

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
    print(v);
end

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
    print(t[k]);
end

See Also Edit

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

Around Wikia's network

Random Wiki