Comparison of Programming Languages (basic Instructions) - Control Flow - Loop Statements

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 }
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 }
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
until notcondition instructions end
begin instructions end while condition
begin instructions end until notcondition
for i in first...last instructions end
first.upto(last-1) { |i| instructions }
for item in set instructions end
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
Do Until notcondition instructions Loop
Do instructions Loop While condition
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
Tab ↹ instructions»
for i in range(first, last):
Tab ↹ instructions
Tab ↹ instructions»
for item in set:
Tab ↹ instructions
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
Seq.iter (fun item -> instructions) set
Standard ML while condition do ( instructions ) (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:

    Nonwhite and working-class women, if they are ever to identify with the organized women’s movement, must see their own diverse experiences reflected in the practice and policy statements of these predominantly white middle-class groups.
    Kimberly Crenshaw (b. 1959)