Recent changes Random page
GAMING
Technology
 
Gaming
Entertainment
Science Fiction
Biggest wikis
Hobbies
Music
See more...

For loop

From Programmer's Wiki

Jump to: navigation, search
 

The for-loop is a special case construct used for indexing over a range of values.

Of particular importance to the for-loop is the starting value (which the body of the loop first sees), an index modification value (which may be implied), and a boundary value.

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.

Languages typically differ on the value of the indexing variable after the loop. The primary choices are: the variable is not accessible, it is undefined, it is the starting value before the loop was executed, it is the ending value of the loop, it is outside the constraint range of the loop. When porting code, especially between languages, it is important to know the implied behavior.

Depending on the language, loops may have a number of ways that they can be terminated or have standard flow interrupted. The function containing the loop may return in the middle of a loop calculation, throw an exception, or jump/goto another code location. Looping constructs often give special control words (like break, next, or continue) to jump to specific control points of the loop. These usually are back to the initializer (to re-run the loop), back to the conditional check (to re-run the iteration), to the end of the loop (to skip the rest of the body and do the next iteration), or to exit the loop entirely (and continue with the following statement).

Loops within loops are potential performance bottlenecks.

Optimizing compilers often unroll loops for speed; if a given statement is to be performed N number of times, the statement is simply replicated N times, and no looping construct is needed. This often streamlines a CPU's instruction pipeline when no comparisons or jumps need to be made.

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.

Languages that use collections or references often have a special form of the for-loop dedicated to walking over a collection. These are called iterators, and it is the iterators job to know the next object in sequence.

Contents

[edit] Code snippets

[edit] Language: BASIC

FOR I = 1 TO 10
  REM DO SOMETHING
NEXT I

FOR I = 1 TO 10 STEP 2
  REM DO SOMETHING, COUNTING BY TWOS
NEXT I

[edit] Language: C

int i = 0;

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

[edit] Language: 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
}

As one can see, Java uses the same syntax for for-loop as C++.

[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: 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] 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

Rate this article:
Share this article: