Syntax
Syntax varies among languages. Most use the simple wordfor
, roughly as follows:
for each item in collection:
do something to item
Language support
ActionScript 3.0
ActionScript supports the ECMAScript 4.0 Standard forfor each .. in
which pulls the value at each index.
for .. in
which pulls the key at each index.
Ada
Ada supports foreach loops as part of the normalC
The C language does not have collections or a foreach construct. However, it has several standard data structures that can be used as collections, and foreach can be made easily with a macro. However, two obvious problems occur: * The macro is unhygienic: it declares a new variable in the existing scope which remains after the loop. * One foreach macro cannot be defined that works with different collection types (e.g., array and linked list) or that is extensible to user types. C string as a collection of chartypeof
typeof, alternately also typeOf, and TypeOf, is an operator provided by several programming languages to determine the data type of a variable. This is useful when constructing programs that must accept multiple types of data without explicitly s ...
(col
used in its place with GCC''
C#
In C#, assuming that myArray is an array of integers:C++
C++11 provides a foreach loop. The syntax is similar to that offor
is begin
and end
functions.
The C++ Standard Library also supports for_each
, that applies each element to a function, which can be any predefined function or a lambda expression. While range-based for is only from the beginning to the end, the range and direction you can change the direction or range by altering the first two parameters.
C++/CLI
The C++/CLI language proposes a construct similar to C#. Assuming that myArray is an array of integers:ColdFusion Markup Language (CFML)
Script syntax
Tag syntax
index
variable does receive the actual value of the array element, not its index.
Common Lisp
Common Lisp provides foreach ability either with the ''dolist'' macro:D
Dart
Object Pascal, Delphi
Foreach support was added inEiffel
The iteration (foreach) form of theacross
.
In this example, every element of the structure my_list
is printed:
ic
is an instance of the library class ITERATION_CURSOR
. The cursor's feature item
provides access to each structure element. Descendants of class ITERATION_CURSOR
can be created to handle specialized iteration algorithms. The types of objects that can be iterated across (my_list
in the example) are based on classes that inherit from the library class ITERABLE
.
The iteration form of the Eiffel loop can also be used as a boolean expression when the keyword loop
is replaced by either all
(effecting some
(effecting my_list
have counts greater than three:
Go
Go's foreach loop can be used to loop over an array, slice, string, map, or channel. Using the two-value form, we get the index/key (first element) and the value (second element):Groovy
Haskell
mapM_
and forM_
(mapM_
with its arguments flipped) frotraverse
(for
with its arguments flipped) and mapM
(forM
with its arguments flipped) froHaxe
Java
InJavaScript
Thefor...in
loop:
Lua
Iterate only through numerical index values:Mathematica
InDo
will simply evaluate an expression for each element of a list, without returning any value.
Table
, which returns the result of each evaluation in a new list.
MATLAB
Mint
For each loops are supported in Mint, possessing the following syntax:for (;;)
or while (true)
Objective-C
Foreach loops, called Fast enumeration, are supported starting inOCaml
__ParaSail_
The___Pascal_
In_ Pascal,_ISO_standard_10206:1990_introduced_iteration_over_ set_types,_thus:__Perl_
In___PHP_
__Python_
for_..._in
_is_the_only_kind_of_for_loop_in_Python,_the_equivalent_to_the_"counter"_loop_found_in_other_languages_is...
enumerate
_function_is_considered_more_"Pythonic":
__R_
for_..._in
_is_the_only_kind_of_for
_loop_in_R,_the_equivalent_to_the_"counter"_loop_found_in_other_languages_is...
__Racket_
for-each
_function:
do-something-with
_is_a_one-argument_function.
__Raku_
In_ Raku,_a_sister_language_to_Perl,_''for''_must_be_used_to_traverse_elements_of_a_list_(''foreach''_is_not_allowed)._The_expression_which_denotes_the_collection_to_loop_over_is_evaluated_in_list-context,_but_not_flattened_by_default,_and_each_item_of_the_resulting_list_is,_in_turn,_aliased_to_the_loop_variable(s). List_literal_example:__Ruby_
__Rust_
The_for
_loop_has_the_structure_for__in__
._It_implicitly_calls_thIntoIterator::into_iter
for
_loop_through_aIterator::next
_method_on_the_iterator_before_executing_the_loop_body._If_Iterator::next
_returns_ Some(_)
,_the_value_inside_is_assigned_to_the_None
,_the_loop_is_terminated.
__Scala_
__Scheme_
do-something-with
_is_a_one-argument_function.
__Smalltalk_
__Swift_
for
…in
_construct_to_iterate_over_members_of_a_collection.
for
…in
_loop_is_often_used_with_the_closed_and_half-open_range_constructs_to_iterate_over_the_loop_body_a_certain_number_of_times.
__SystemVerilog_
foreach
_keyword.
A_trivial_example_iterates_over_an_array_of_integers:
A_more_complex_example_iterates_over_an_associative_array_of_arrays_of_integers:
__Tcl_
_i
_assumes_sequential_values_of_the_first_list,_j
_sequential_values_of_the_second_list:
__Visual_Basic_.NET_
__Windows_
__Conventional_command_processor_
Invoke_a_hypothetical_frob
_command_three_times,_giving_it_a_color_name_each_time.
__Windows_PowerShell_
__XSLT_
__See_also_
*___References_
{{Reflist, 2 Articles_with_example_Ada_code Articles_with_example_Perl_code Articles_with_example_PHP_code Articles_with_example_Python_(programming_language)_code Articles_with_example_Racket_code Articles_with_example_Smalltalk_code Articles_with_example_Tcl_code Control_flow Programming_language_comparisons Articles_with_example_Java_code Articles_with_example_Haskell_code ru:Цикл_просмотраhtml" ;"title="1;2;3;4, ];; or in short way:ParaSail
ThePascal
In Pascal, ISO standard 10206:1990 introduced iteration over set types, thus:Perl
InPHP
Python
for ... in
is the only kind of for loop in Python, the equivalent to the "counter" loop found in other languages is...
enumerate
function is considered more "Pythonic":
R
for ... in
is the only kind of for
loop in R, the equivalent to the "counter" loop found in other languages is...
Racket
for-each
function:
do-something-with
is a one-argument function.
Raku
In Raku, a sister language to Perl, ''for'' must be used to traverse elements of a list (''foreach'' is not allowed). The expression which denotes the collection to loop over is evaluated in list-context, but not flattened by default, and each item of the resulting list is, in turn, aliased to the loop variable(s). List literal example:Ruby
Rust
Thefor
loop has the structure for in
. It implicitly calls thIntoIterator::into_iter
for
loop through aIterator::next
method on the iterator before executing the loop body. If Iterator::next
returns Some(_)
, the value inside is assigned to the None
, the loop is terminated.
Scala
Scheme
do-something-with
is a one-argument function.
Smalltalk
Swift
for
…in
construct to iterate over members of a collection.
for
…in
loop is often used with the closed and half-open range constructs to iterate over the loop body a certain number of times.
SystemVerilog
foreach
keyword.
A trivial example iterates over an array of integers:
A more complex example iterates over an associative array of arrays of integers:
Tcl
Tcl uses foreach to iterate over lists. It is possible to specify more than one iterator variable, in which case they are assigned sequential values from the list. It is also possible to iterate over more than one list simultaneously. In the followingi
assumes sequential values of the first list, j
sequential values of the second list:
Visual Basic .NET
Windows
Conventional command processor
Invoke a hypotheticalfrob
command three times, giving it a color name each time.
Windows PowerShell
XSLT
See also
*References
{{Reflist, 2 Articles with example Ada code Articles with example Perl code Articles with example PHP code Articles with example Python (programming language) code Articles with example Racket code Articles with example Smalltalk code Articles with example Tcl code Control flow Programming language comparisons Articles with example Java code Articles with example Haskell code ru:Цикл просмотра>1;2;3;4, ; or in short way:__ParaSail_
The___Pascal_
In_ Pascal,_ISO_standard_10206:1990_introduced_iteration_over_ set_types,_thus:__Perl_
In___PHP_
__Python_
for_..._in
_is_the_only_kind_of_for_loop_in_Python,_the_equivalent_to_the_"counter"_loop_found_in_other_languages_is...
enumerate
_function_is_considered_more_"Pythonic":
__R_
for_..._in
_is_the_only_kind_of_for
_loop_in_R,_the_equivalent_to_the_"counter"_loop_found_in_other_languages_is...
__Racket_
for-each
_function:
do-something-with
_is_a_one-argument_function.
__Raku_
In_ Raku,_a_sister_language_to_Perl,_''for''_must_be_used_to_traverse_elements_of_a_list_(''foreach''_is_not_allowed)._The_expression_which_denotes_the_collection_to_loop_over_is_evaluated_in_list-context,_but_not_flattened_by_default,_and_each_item_of_the_resulting_list_is,_in_turn,_aliased_to_the_loop_variable(s). List_literal_example:__Ruby_
__Rust_
The_for
_loop_has_the_structure_for__in__
._It_implicitly_calls_thIntoIterator::into_iter
for
_loop_through_aIterator::next
_method_on_the_iterator_before_executing_the_loop_body._If_Iterator::next
_returns_ Some(_)
,_the_value_inside_is_assigned_to_the_None
,_the_loop_is_terminated.
__Scala_
__Scheme_
do-something-with
_is_a_one-argument_function.
__Smalltalk_
__Swift_
for
…in
_construct_to_iterate_over_members_of_a_collection.
for
…in
_loop_is_often_used_with_the_closed_and_half-open_range_constructs_to_iterate_over_the_loop_body_a_certain_number_of_times.
__SystemVerilog_
foreach
_keyword.
A_trivial_example_iterates_over_an_array_of_integers:
A_more_complex_example_iterates_over_an_associative_array_of_arrays_of_integers:
__Tcl_
_i
_assumes_sequential_values_of_the_first_list,_j
_sequential_values_of_the_second_list:
__Visual_Basic_.NET_
__Windows_
__Conventional_command_processor_
Invoke_a_hypothetical_frob
_command_three_times,_giving_it_a_color_name_each_time.
__Windows_PowerShell_
__XSLT_
__See_also_
*___References_
{{Reflist, 2 Articles_with_example_Ada_code Articles_with_example_Perl_code Articles_with_example_PHP_code Articles_with_example_Python_(programming_language)_code Articles_with_example_Racket_code Articles_with_example_Smalltalk_code Articles_with_example_Tcl_code Control_flow Programming_language_comparisons Articles_with_example_Java_code Articles_with_example_Haskell_code ru:Цикл_просмотраhtml" ;"title="1;2;3;4, ];; or in short way:ParaSail
ThePascal
In Pascal, ISO standard 10206:1990 introduced iteration over set types, thus:Perl
InPHP
Python
for ... in
is the only kind of for loop in Python, the equivalent to the "counter" loop found in other languages is...
enumerate
function is considered more "Pythonic":
R
for ... in
is the only kind of for
loop in R, the equivalent to the "counter" loop found in other languages is...
Racket
for-each
function:
do-something-with
is a one-argument function.
Raku
In Raku, a sister language to Perl, ''for'' must be used to traverse elements of a list (''foreach'' is not allowed). The expression which denotes the collection to loop over is evaluated in list-context, but not flattened by default, and each item of the resulting list is, in turn, aliased to the loop variable(s). List literal example:Ruby
Rust
Thefor
loop has the structure for in
. It implicitly calls thIntoIterator::into_iter
for
loop through aIterator::next
method on the iterator before executing the loop body. If Iterator::next
returns Some(_)
, the value inside is assigned to the None
, the loop is terminated.
Scala
Scheme
do-something-with
is a one-argument function.
Smalltalk
Swift
for
…in
construct to iterate over members of a collection.
for
…in
loop is often used with the closed and half-open range constructs to iterate over the loop body a certain number of times.
SystemVerilog
foreach
keyword.
A trivial example iterates over an array of integers:
A more complex example iterates over an associative array of arrays of integers:
Tcl
Tcl uses foreach to iterate over lists. It is possible to specify more than one iterator variable, in which case they are assigned sequential values from the list. It is also possible to iterate over more than one list simultaneously. In the followingi
assumes sequential values of the first list, j
sequential values of the second list:
Visual Basic .NET
Windows
Conventional command processor
Invoke a hypotheticalfrob
command three times, giving it a color name each time.
Windows PowerShell
XSLT
See also
*References
{{Reflist, 2 Articles with example Ada code Articles with example Perl code Articles with example PHP code Articles with example Python (programming language) code Articles with example Racket code Articles with example Smalltalk code Articles with example Tcl code Control flow Programming language comparisons Articles with example Java code Articles with example Haskell code ru:Цикл просмотра>1;2;3;4, ;ParaSail
ThePascal
In Pascal, ISO standard 10206:1990 introduced iteration over set types, thus:Perl
InPHP
Python
for ... in
is the only kind of for loop in Python, the equivalent to the "counter" loop found in other languages is...
enumerate
function is considered more "Pythonic":
R
for ... in
is the only kind of for
loop in R, the equivalent to the "counter" loop found in other languages is...
Racket
for-each
function:
do-something-with
is a one-argument function.
Raku
In Raku, a sister language to Perl, ''for'' must be used to traverse elements of a list (''foreach'' is not allowed). The expression which denotes the collection to loop over is evaluated in list-context, but not flattened by default, and each item of the resulting list is, in turn, aliased to the loop variable(s). List literal example:Ruby
Rust
Thefor
loop has the structure for in
. It implicitly calls thIntoIterator::into_iter
for
loop through aIterator::next
method on the iterator before executing the loop body. If Iterator::next
returns Some(_)
, the value inside is assigned to the None
, the loop is terminated.
Scala
Scheme
do-something-with
is a one-argument function.
Smalltalk
Swift
for
…in
construct to iterate over members of a collection.
for
…in
loop is often used with the closed and half-open range constructs to iterate over the loop body a certain number of times.
SystemVerilog
foreach
keyword.
A trivial example iterates over an array of integers:
A more complex example iterates over an associative array of arrays of integers:
Tcl
Tcl uses foreach to iterate over lists. It is possible to specify more than one iterator variable, in which case they are assigned sequential values from the list. It is also possible to iterate over more than one list simultaneously. In the followingi
assumes sequential values of the first list, j
sequential values of the second list:
Visual Basic .NET
Windows
Conventional command processor
Invoke a hypotheticalfrob
command three times, giving it a color name each time.
Windows PowerShell
XSLT
See also
*References
{{Reflist, 2 Articles with example Ada code Articles with example Perl code Articles with example PHP code Articles with example Python (programming language) code Articles with example Racket code Articles with example Smalltalk code Articles with example Tcl code Control flow Programming language comparisons Articles with example Java code Articles with example Haskell code ru:Цикл просмотра