For loop

From CodeCodex

Revision as of 07:36, 26 December 2009 by 65.7.166.232 (Talk)

In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement.

Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop.

Java

<highlightsyntax language="java"> for (int i = 0; i < 5; i++) {

 System.out.println(i);

} </highlightsyntax> This will print:

0
1
2
3
4


C

<highlightsyntax language="c"> for (int i = 0; i < 5; i++) {

 System.out.println(i);

} </highlightsyntax> This will print:

0
1
2
3
4


Basic

<highlightsyntax> FOR I = 1 TO 4

  PRINT I

NEXT I </highlightsyntax> This will print

1
2
3
4

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 fuck this that value is out of scope when the loop exits.

Groovy

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

Javascript

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

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;

Perl

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

Perl6

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

PHP

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

Python

for i in range(10):
    # Do something

Ruby

for i in (1...10)
  #add something
end

Or alternatively

0.upto(9){|i|
  #do something
}

Or even something else

(1...10).each{|i|
  #do something
}


Tcl

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

IDL

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

Visual Basic

For i As Integer = 0 To 9
    ' Do something
Next

WinBatch

; WinBatch. Examples for "For-Next" and "ForEach-Next" loop statements.

; The "For-Next" loop will be executed a specified number of times.
; Example 1. Standard usage.
intSum1 = 0
For intIterator = 0 To 10
   intSum1 = intSum1 + intIterator
Next
Message ("Sum1", intSum1) ; "55"

; Example 2. Using negative increment.
intSum2 = 0
For intIterator = 10 To 0 By -1
   intSum2 = intSum2 + intIterator
Next
Message ("Sum2", intSum2) ; "55"

; Example 3.
intSum3 = 0
For intIterator = -10 To 11 By 3
   intSum3 = intSum3 + intIterator
Next
Message ("Sum3", intSum3) ; "4"

; Example 4.
intSum4 = 0
intCount = 0
For intIterator = 11 To -10 By -1
   intSum4 = intSum4 + intIterator
   intCount = intCount + 1
Next
Message ("Sum4 / Count", intSum4 : " / " : intCount) ; "11 / 22"


; Example 5.
; The "ForEach-Next" loop is similar to the "For-Next" loop,
; but it executes the statement block for each element in a collection,
; instead of a specified number of times.

objShell = ObjectCreate ("Shell.Application")
If objShell.Windows.Count
   intI = 0
   ForEach objWindow In objShell.Windows
      intI = intI + 1
      Message ("Windowname " : intI, objWindow.name)
   Next
EndIf
Drop (objShell)


; Example 6.
; The statements "Continue" and "Break" can be used to immediately stop the execution of statements in the inner loop block.
; "Continue" re-evaluates the initial "For ..." expression and continues the loop as applicable.
; "Break" exits the loop and transfers the program flow control to the statement behind the "Next" statement.

intMin = 0
intMax = 9
For intElem = intMin To intMax
   Display (2, "Demo: For-Next ... loop in", intElem)  ; intElem =  0, 1, 2, 3, 4, 5, 6, 7, 8.
   ; Do something here.
   If intElem == 3 Then Continue               ; Jump up, next loop.
   ; Do something here.
   If intElem == 4 Then Continue               ; Jump up, next loop.
   ; Do something here.
   If intElem == 8 Then Break                  ; Jump out.
   ; Do something here.
   Display (2, "Demo: For-Next ... loop out", intElem) ; intElem =  0, 1, 2, 5, 6, 7.
Next
Display (2, "Demo: For-Next ... after loop", intElem) ; intElem =  8.

Exit
; This WinBatch code example was written by Detlev Dalitz.20090719.1050.CEST
;;;

Pseudo Code

initialize_index
while ( some_condition_is_true ) {
  do_body_of_loop
  modify_index
}

Zsh

for i in {0..9}
do
   # Do something
done

Or

for i in {0..9};  # Do something

Or

for ((i = 0 ; i < 10 ; i++))
do
  # Do something
done