Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 10:07, 15 August 2025 by Dylan (talk | contribs) (new: info, example, iterating a collection)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A for loop is an iteration structure in code which allows us to loop through a code block and easily increment a variable each time.

It is a form of definite iteration, or count-controlled iteration.

In the below C Sharp code, we loop through the numbers 1-5 and print each number to the console:

for (int i = 1; i <= 5; i++)
{
    Console.WriteLine(i);
}

Iterating a collection

edit edit source

If you instead want to loop over a collection, you can either use a regular for loop and iterate over each index of the collection, or you can use a for each loop to iterate over the elements themselves.

See: For each loop