0Pricing
C# Academy · Lesson

Looping Over Arrays

for and foreach.

Looping Over Arrays is a free C# Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Loop?

Arrays often hold many values, and you rarely want to type each index by hand. A loop repeats an action for every element automatically.

This lets you print, sum, or check all values with just a few lines.

The for Loop

A for loop uses a counter. Start it at 0 and continue while the counter is less than Length, increasing by one each time.

The counter doubles as the index, so you can access each element.

int[] nums = { 10, 20, 30 };
for (int i = 0; i < nums.Length; i++)
{
    Console.WriteLine(nums[i]);
}

A Full for Program

This complete program loops through colors and prints each one on its own line.

Run it and notice how the same line of code handles every element.

string[] colors = { "red", "green", "blue" };
for (int i = 0; i < colors.Length; i++)
{
    Console.WriteLine(colors[i]);
}

The foreach Loop

When you only need the values and not the index, foreach is simpler. It hands you each element in order.

There is no counter to manage, which means fewer chances for mistakes.

string[] colors = { "red", "green", "blue" };
foreach (string c in colors)
{
    Console.WriteLine(c);
}

foreach With var

You can use var in a foreach to let C# figure out the element type.

This keeps the loop short and still type-safe.

int[] nums = { 1, 2, 3 };
foreach (var n in nums)
{
    Console.WriteLine(n * 2);
}

for vs foreach

Use for when you need the index, want to skip elements, or must modify the array. Use foreach when you simply read each value.

You cannot change the loop variable of a foreach to write back into the array; use for for that.

int[] nums = { 1, 2, 3 };
for (int i = 0; i < nums.Length; i++)
{
    nums[i] = nums[i] + 1; // updates the array
}
Console.WriteLine(nums[0]); // 2

Summing Elements

A common task is adding all values. Keep a running total and add each element inside the loop.

This pattern works for counts, totals, and averages.

int[] nums = { 4, 8, 15 };
int total = 0;
foreach (int n in nums)
{
    total += n;
}
Console.WriteLine(total); // 27

Finding the Maximum

To find the largest value, start by assuming the first element is the max, then compare every other element.

Whenever you find a bigger value, update the max.

int[] nums = { 3, 9, 2, 7 };
int max = nums[0];
foreach (int n in nums)
{
    if (n > max) max = n;
}
Console.WriteLine(max); // 9

Searching for a Value

You can loop to check whether a value exists. Break out of the loop as soon as you find a match to save work.

A flag variable remembers whether the item was found.

string[] names = { "Ana", "Bo", "Cy" };
bool found = false;
foreach (string n in names)
{
    if (n == "Bo") { found = true; break; }
}
Console.WriteLine(found); // True

Printing With Index

Sometimes you want to show the position too. A for loop gives you the index alongside the value.

This is great for numbered lists.

string[] tasks = { "Wake", "Eat", "Code" };
for (int i = 0; i < tasks.Length; i++)
{
    Console.WriteLine((i + 1) + ". " + tasks[i]);
}

Looping a Range

You do not have to loop the whole array. Change the start or end of a for loop to visit only part of it.

Here we skip the first element by starting at index 1.

int[] nums = { 10, 20, 30, 40 };
for (int i = 1; i < nums.Length; i++)
{
    Console.WriteLine(nums[i]); // 20, 30, 40
}

Quick Check

Test your loop knowledge.

Recap

You learned to walk arrays with for (counter and index) and foreach (each value directly).

Use for when you need the index or want to modify elements, and foreach for clean read-only passes. You also summed, searched, and found the max. Next you will work with grids using multidimensional arrays.

Frequently asked questions

Is the “Looping Over Arrays” lesson free?

Yes — the full text of “Looping Over Arrays” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Looping Over Arrays”?

for and foreach. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Looping Over Arrays” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring Arrays
  2. Indexing and Length
  3. Looping Over Arrays
  4. Multidimensional Arrays
← Back to C# Academy