for / foreach
Counting and iterating over collections.
Syntax
for (int i=0;i<n;i++) // foreach (var x in collection)
Description
The classic for loop runs a specific number of times. foreach visits every element of a collection in order — you don't have to deal with indices.
Example
Loading editor…
Expected output
1 2 3 a b
When to use?
List processing, repetitive tasks.