For loop
From Programmer's Wiki
a for loop is a loop that is used when when you need to have a count of how many times the loop has executed, or want code to execute a specific number of times.
[edit] 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 ends the loop
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 end "i < 10"
- 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.
[edit] 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.
[edit] Code snippets
[edit] Language: BASIC
FOR I = 1 TO 10 REM Do something NEXT I FOR J = 1 TO 10 STEP 2 PRINT J NEXT I END
[edit] Language: C
int i;
for( i = 0; i < 10; ++i ) {
/* Do something */
}
[edit] 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.
[edit] Language: Groovy
for ( i in 0..20 ) {
// Do something
}
[edit] Language: Java
for ( int i = 0; i < 20; i++ ) {
// Do something
}
[edit] Language: Javascript
for(var i=0; i<10; i++) {
// Do something
}
[edit] 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 usage, but can be any ordinal type (included defined enumerated types). For example:
for loop:='a' to 'd' do
begin
{Do something}
end;
[edit] Language: Perl
for(my $i=0; $i<10; $i++) {
# Do something
}
[edit] Language: Perl6
In Perl6 it is loop instead of for. See Perl6 Loop
loop( my Int $i = 0; $i < 10; $i++ ) {
# Do something
}
[edit] Language: PHP
for($i=0; $i<10; $i++) {
# Do something
}
[edit] Language: Python
for i in range(10):
# Do something
[edit] Language: Tcl
for {set i 0} {$i < 10} {incr i} {
# Do something
}
[edit] Language: IDL
for i=0L, 10-1 do begin ; Do something endfor
[edit] Language: Visual Basic
For i As Integer = 0 To 9
' Do something
Next
[edit] Reduction
Looping constructs can always be reduced to the following pseudo code:
initialize_index
while ( some_condition_is_true ) {
do_body_of_loop
modify_index
}
[edit] See Also
|
