Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Ada
- C
- C
- Caml
- Clojure
- Cobol
- C++
- C#
- D
- D
- Dart
- Elixir
- Elixir
- Erlang
- Erlang
- Fortran
- Go
- Groovy
- Groovy
- Groovy
- Haskell
- JS
- JS
- JS
- JS
- JS
- Java
- Java
- Java
- Java
- Java
- Java
- Kotlin
- Kotlin
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- PHP
- Pascal
- Perl
- Perl
- Python
- Python
- Python
- Ruby
- Ruby
- Rust
- Rust
- Scala
- Scala
- Scala
- Scheme
- Scheme
- Scheme
- Smalltalk
- VB
for Item of Items loop
Do_Something (Item);
end loop;
for (size_t i = 0; i < sizeof(items) / sizeof(items[0]); i++) {
DoSomethingWith(&items[i]);
}
sizeof the array divided by the size of the first element computes the number of elements, often defined as macro ARRAY_SIZE
for (unsigned int i = 0 ; i < items_length ; ++i){
Item* x = &(items[i]);
DoSomethingWith(x);
}
items_length type is: unsigned int
DoSomethingWith prototype is: void DoSomethingWith(Item*);
DoSomethingWith prototype is: void DoSomethingWith(Item*);
items |> List.iter do_something
(map do-something items)
do-something should be a function that receives a single argument. map returns a lazy seq, which means the function do-something won't be applied until the results are needed.
IDENTIFICATION DIVISION.
PROGRAM-ID. list.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LIST.
03 X OCCURS 5 TIMES
INDEXED BY i PIC 9.
PROCEDURE DIVISION.
PERFORM VARYING i FROM 1 BY 1 UNTIL i > 5
DISPLAY X(i)
END-PERFORM
STOP RUN.
for(const auto &x : items) {
doSomething(x);
}
This "range-based for loop" exists since C++11.
foreach(x; items) {
doSomethingWith(x);
}
items.forEach(doSomething);
[do_something(X) || X <- Items]
lists:foreach(fun do_something/1, Items).
elemental subroutine foo(x)
real, intent(in) :: x
end subroutine foo
call foo(x)
for _, x := range items {
doSomething(x)
}
You have to explicitly ignore the index loop variable, with the blank identifier _
items.each { x -> doSomething(x) }
for(x in items) doSomething(x)
for (var i = 0; i<items.length; i++) {
var x = items[i];
doSomething(x);
}
for(int i=0;i<items.length;i++){
doSomething( items[i] );
}
This syntax is now "old-school" since the foreach construct introduced in Java 5.
T t;
Iterator<T> i = items.listIterator();
while (i.hasNext()) {
t = i.next();
}
The `ListIterator` allows for backward iteration, and mutation.
for (T x : items) {}
items.stream().forEach(item -> doSomething(item));
This syntax uses the streams api introduced in Java 8.
for (Item x: items) {
doSomething(x);
}
This syntax is allowed since Java 5.
It works with arrays or collections as well.
Item is the type of the elements.
It works with arrays or collections as well.
Item is the type of the elements.
T t;
Iterator<T> i = items.iterator();
while (i.hasNext()) {
t = i.next();
}
items.forEach(::doSomething)
Using a function reference.
(map nil #'something items)
for _, x in ipairs(items) do
print(x)
end
for (id x in items) _doSomething(x);
in case items contains objects of known type, it can/should be used, like for(NSString *str in items) ...
array_map(func, $items);
Function does need to be a real function and not a language construct, so you can't directly map _- (the unary negation operator) or other function like constructs like echo or print
Do note, this fails for iteratable non-arrays, the foreach based approach is better in that situation.
Do note, this fails for iteratable non-arrays, the foreach based approach is better in that situation.
foreach ($items as $x){
doSomething( $x );
}
for x in items do ;
do_something($_) for @items;
This performs a single operation on each element
for my $x (@items) {
do_something($x);
more_stuffs($x);
}
This construct is best for multiple operations on $x
f = lambda x: ...
for x in items: f(x)
[do_something(x) for x in items]
for x in items:
doSomething( x )
for x in items {
do_something(x);
}
items.into_iter().for_each(|x| do_something(x));
items.foreach(doSomething)
when the method you're calling on each item in the collection only takes a single parameter, you can omit specifying it.
for {
x <- items
} doSomething(x)
items.foreach{doSomething(_)}
(map doSomething items)
(define (doSomething x)
(if (not (null? x))
(begin
(display "Item=")
(display (car x))
(newline)
(doSomething (cdr x)))))
Iteration is achieved by recursion.
(define (sum a b)
(sum-iter a b 0))
(define (sum-iter index n sum)
(if (= n index)
(+ sum index)
(+ (sum-iter n (+ index 1) (+ sum index)))
)
items do: [:x | x doSomething]