Glossary: for loop - A loop construct used to count iterations, potentially to limit them.
Explanation
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
- 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
continuestatement, then the incremental statement (i++) will run only if we are using theforversion.
Code snippets
Language: ActionScript 3
for ( var counter:int = 1; counter <= 5; counter++ )
// Do something;
Language: BASIc
FOR I = 1 TO 10
REM Do something
NEXT I
FOR J = 1 TO 10 STEP 2
PRINT J
NEXT J
END
Language: Bash
# 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
int i;
for( i = 0; i < 10; i++ ) {
/* Do something */
}
Language: C++/C#/
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
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
for ( int i = 0; i < 20; i++ ) {
// Do something
}
for ( int i : new int[]{1,2,3,4} ) {
// do something
}
Language: JavaScript
for( var i = 0; i < 10; i++ ) {
// Do something
}
Language: Pascal
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
for( my $i=0; $i < 10; $i++ ) {
# Do something
}
Language: Perl6
In Perl6 it is loop instead of for. See Perl6 Loop
loop( my Int $i = 0; $i < 10; $i++ ) {
# Do something
}
Language: PHP
for( $i = 0; $i < 10; $i++ ) {
# Do something
}
Language: Python
for i in range(10):
# Do something
Language: Tcl
for {set i 0} {$i < 10} {incr i} {
# Do something
}
Language: IDL
for i=0L, 10-1 do begin
; Do something
endfor
Language: Visual Basic
For i As Integer = 0 To 9
' Do something
Next
for count= 1-10
addition
end for
Reduction
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
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
|