Concept

Foreach loop

Summary
In computer programming, foreach loop (or for-each loop) is a control flow statement for traversing items in a collection. is usually used in place of a standard loop statement. Unlike other loop constructs, however, loops usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages, an iterator, even if implicit, is often used as the means of traversal. The statement in some languages has some defined order, processing each item in the collection from the first to the last. The statement in many other languages, especially array programming languages, does not have any particular order. This simplifies loop optimization in general and in particular allows vector processing of items in the collection concurrently. Syntax varies among languages. Most use the simple word for, roughly as follows: for each item in collection: do something to item Programming languages which support foreach loops include ABC, ActionScript, Ada, C++11, C#, ColdFusion Markup Language (CFML), Cobra, D, Daplex (query language), Delphi, ECMAScript, Erlang, Java (since 1.5), JavaScript, Lua, Objective-C (since 2.0), ParaSail, Perl, PHP, Prolog, Python, R, REALbasic, Rebol, Red, Ruby, Scala, Smalltalk, Swift, Tcl, tcsh, Unix shells, Visual Basic .NET, and Windows PowerShell. Notable languages without foreach are C, and C++ pre-C++11. ActionScript supports the ECMAScript 4.0 Standard for for each .. in which pulls the value at each index. var foo:Object = { "apple":1, "orange":2 }; for each (var value:int in foo) { trace(value); } // returns "1" then "2" It also supports for .. in which pulls the key at each index. for (var key:String in foo) { trace(key); } // returns "apple" then "orange" Ada supports foreach loops as part of the normal for loop. Say X is an array: for I in X'Range loop X (I) := Get_Next_Element; end loop; This syntax is used on mostly arrays, but will also work with other types when a full iteration is needed.
About this result
This page is automatically generated and may contain information that is not correct, complete, up-to-date, or relevant to your search query. The same applies to every other page on this website. Please make sure to verify the information with EPFL's official sources.