Foreach loop

This simplifies loop optimization in general and in particular allows vector processing of items in the collection concurrently.

Most use the simple word for, although other use the more logical word foreach, roughly as follows: Programming languages which support foreach loops include ABC, ActionScript, Ada, C++ (since 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,[2] Python, R, REALbasic, Rebol,[3] Red,[4] Ruby, Scala, Smalltalk, Swift, Tcl, tcsh, Unix shells, Visual Basic (.NET), and Windows PowerShell.

Qt, a C++ framework, offers a macro providing foreach loops[11] using the STL iterator interface: Boost, a set of free peer-reviewed portable C++ libraries also provides foreach loops:[12] The C++/CLI language proposes a construct similar to C#.

Common Lisp provides foreach ability either with the dolist macro: or the powerful loop macro to iterate on more data types and even with the mapcar function: or Foreach support was added in Delphi 2005, and uses an enumerator variable that must be declared in the var section.

The iteration (foreach) form of the Eiffel loop construct is introduced by the keyword across.

In this example, every element of the structure my_list is printed: The local entity ic is an instance of the library class ITERATION_CURSOR.

This iteration is a boolean expression which is true if all items in my_list have counts greater than three: The following is true if at least one item has a count greater than three: Go's foreach loop can be used to loop over an array, slice, string, map, or channel.

For unordered iteration over the keys in an object, JavaScript features the for..in loop: To limit the iteration to the object's own properties, excluding those inherited through the prototype chain, it's often useful to add a hasOwnProperty() test (or a hasOwn() test if supported).

[19] Alternatively, the Object.keys() method combined with the for..of loop can be used for a less verbose way to iterate over the keys of an object.

[22] Foreach loops, called Fast enumeration, are supported starting in Objective-C 2.0.

List literal example: Array examples: Hash example: Direct modification of collection members: It is also possible to extract both keys and values using the alternate syntax: Direct modification of collection members: Python's tuple assignment, fully available in its foreach loop, also makes it trivial to iterate on (key, value) pairs in dictionaries: As for ... in is the only kind of for loop in Python, the equivalent to the "counter" loop found in other languages is... ... although using the enumerate function is considered more "Pythonic": As for ... in is the only kind of for loop in R, the equivalent to the "counter" loop found in other languages is... or using the conventional Scheme for-each function: do-something-with is a one-argument function.

In Raku, a sister language to Perl, for must be used to traverse elements of a list (foreach is not allowed).

It implicitly calls the IntoIterator::into_iter method on the expression, and uses the resulting value, which must implement the Iterator trait.

SystemVerilog supports iteration over any vector or array type of any dimensionality using the foreach keyword.

foreach loops are almost always used to iterate over items in a sequence of elements.