For loop

From CodeCodex

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

for (int i = 0; i < 5; i++) {
  System.out.println(i);
}

This will print:

0
1
2
3
4

C[edit]

for (int i = 0; i < 5; i++) {
  printf("%d\n",i);
}

This will print:

0
1
2
3
4

Basic[edit]

FOR I = 1 TO 4
   PRINT I
NEXT I

This will print

1
2
3
4

C++/C#[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 this value is out of scope when the loop exits.

Groovy[edit]

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

Javascript[edit]

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

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 usage, but can be any ordinal type (included defined enumerated types). For example:

for loop:='a' to 'd' do
begin
   {Do something}
end;

Perl[edit]

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

Perl6[edit]

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

PHP[edit]

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

Python[edit]

for i in range(10):
    # Do something

Ruby[edit]

for i in 0..9
  #do something
end

Or alternatively

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

Or even something else

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

Or

10.times do |i|
  #do something
end

The loop counter(i) repeats 10 times from 0 to 9.

Tcl[edit]

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

IDL[edit]

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

Visual Basic[edit]

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

WinBatch[edit]

; 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")
intI = 0
ForEach objWindow In objShell.Windows
   intI = intI + 1
   Message ("Windowname " : intI, objWindow.name)
Next
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[edit]

initialize_index
while ( some_condition_is_true ) {
  do_body_of_loop
  modify_index
}

Zsh[edit]

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