Loop Statements
while | do while | for i = first to last | foreach | |
---|---|---|---|---|
ALGOL 68 | «for index» «from first» «by increment» «to last» «while condition» do statements od | for key «to upb list» do «typename val=list;» statements od | ||
«while condition»
do statements od |
«while statements; condition»
do statements od |
«for index» «from first» «by increment» «to last» do statements od | ||
C (C99) | while (condition) { instructions } | do { instructions } while (condition) | for («type» i = first; i <= last; ++i) { instructions } | |
Objective-C | for (type item in set) { instructions } | |||
C++ (STL) | «std::»for_each(start, end, function)
(C++11) for (type item : set) { instructions } |
|||
C# | foreach (type item in set) { instructions } | |||
Java | for (type item : set) { instructions } | |||
JavaScript | for (var i = first; i <= last; i++) { instructions } | for (var index in set) { instructions } or for each (var item in set) { instructions } (JS 1.6+) |
||
PHP | foreach (range(first, last-1) as $i) { instructions } or for ($i = first; $i <= last; $i++) { instructions } |
foreach (set as item) { instructions } or foreach (set as key => item) { instructions } |
||
Windows PowerShell | for ($i = first; $i -le last; $i++) { instructions } | foreach (item in set) { instructions using item } | ||
D | foreach (i; first ... last) { instructions } | foreach («type» item; set) { instructions } | ||
Go | for condition { instructions } | for i := first; i <= last; i++ { instructions } | for key, item := range set { instructions } | |
Perl | while (condition) { instructions } or until (notcondition) { instructions } |
do { instructions } while (condition) or do { instructions } until (notcondition) |
for«each» «$i» (0 .. N-1) { instructions } or for ($i = first; $i <= last; $i++) { instructions } |
for«each» «$item» (set) { instructions } |
Perl 6 | while condition { instructions } or until notcondition { instructions } |
repeat { instructions } while condition or repeat { instructions } until notcondition |
for first..last -> $i { instructions } or loop ($i = first; $i <= last; $i++) { instructions } |
for set« -> $item» { instructions } |
Ruby | while condition
instructions
end or until notcondition instructions end |
begin
instructions
end while condition or begin instructions end until notcondition |
for i in first...last
instructions
end or first.upto(last-1) { |i| instructions } |
for item in set
instructions
end or set.each { |item| instructions } |
Smalltalk | conditionBlock whileTrue: loopBlock | loopBlock doWhile: conditionBlock | first to: last do: loopBlock | collection do: loopBlock |
Common Lisp | (loop
while condition
do
instructions)
or (do (notcondition) instructions) |
(loop do instructions while condition) | (loop
for i from first to last «by 1»
do
instructions)
or (dotimes (i N) instructions) or (do ((i first (1+ i))) ((>= i last)) instructions) |
(loop
for item in set
do
instructions)
or (dolist (item set) instructions) or (mapc function list) or (map 'type function sequence) |
Scheme | (do (notcondition) instructions) or (let loop (if condition (begin instructions (loop)))) |
(let loop (instructions (if condition (loop)))) | (do ((i first (+ i 1))) ((>= i last)) instructions) or (let loop ((i first)) (if (< i last) (begin instructions (loop (+ i 1))))) |
(for-each (lambda (item) instructions) list) |
ISLISP | (while condition instructions) | (tagbody loop instructions (if condition (go loop)) | (for ((i first (+ i 1))) ((>= i last)) instructions) | (mapc (lambda (item) instructions) list) |
Pascal | while condition do begin instructions end | repeat instructions until notcondition; | for i := first «step 1» to last do begin instructions end; | |
Visual Basic | Do While condition
instructions
Loop or Do Until notcondition instructions Loop |
Do
instructions
Loop While condition or Do instructions Loop Until notcondition |
For i = first To last «Step 1» instructions Next i | For Each item In set instructions Next item |
Visual Basic .NET | For i «As type» = first To last «Step 1» instructions Next i | For Each item As type In set instructions Next item | ||
Python | while condition : Tab ↹ instructions «else: Tab ↹ instructions» |
for i in range(first, last): Tab ↹ instructions «else: Tab ↹ instructions» |
for item in set: Tab ↹ instructions «else: Tab ↹ instructions» |
|
S-Lang | while (condition) { instructions } «then optional-block» | do { instructions } while (condition) «then optional-block» | for (i = first; i < last; i++) { instructions } «then optional-block» | foreach item(set) «using (what)» { instructions } «then optional-block» |
Fortran | DO WHILE (condition) instructions ENDDO | DO
instructions IF (condition) EXIT ENDDO |
DO I = first,last instructions ENDDO | |
Forth | BEGIN « instructions » condition WHILE instructions REPEAT | BEGIN instructions condition UNTIL | limit start DO instructions LOOP | |
OCaml | while condition do instructions done | for i = first to last-1 do instructions done | Array.iter (fun item -> instructions) array List.iter (fun item -> instructions) list |
|
F# | while condition do Tab ↹ instructions |
for i = first to last-1 do Tab ↹ instructions |
for item in set do Tab ↹ instructions or Seq.iter (fun item -> instructions) set |
|
Standard ML | while condition do ( instructions ) | Array.app (fn item => instructions) array app (fn item => instructions) list |
||
Haskell (GHC) | Control.Monad.forM_ (\i -> do instructions) | Control.Monad.forM_ list (\item -> do instructions) | ||
Eiffel | from setup until condition loop instructions end |
^a "step n" is used to change the loop interval. If "step" is omitted, then the loop interval is 1.
Read more about this topic: Comparison Of Programming Languages (basic Instructions), Control Flow
Famous quotes containing the word statements:
“He admired the terrible recreative power of his memory. It was only with the weakening of this generator whose fecundity diminishes with age that he could hope for his torture to be appeased. But it appeared that the power to make him suffer of one of Odettes statements seemed exhausted, then one of these statements on which Swanns spirit had until then not dwelled, an almost new word relayed the others and struck him with new vigor.”
—Marcel Proust (18711922)