DEV Community

Cover image for C# Loops - Part 1: Loop Types & For Loop Tutorial
Grant Riordan
Grant Riordan

Posted on • Edited on

C# Loops - Part 1: Loop Types & For Loop Tutorial

Ok so we've covered a lot of the basics already, and here's yet another core feature of any programming language; the Loop.

Kinds of loop

There are a few ways of writing loops in C#, the

  • for loop
  • for each loop
  • do..while loop
  • while loop

For Loop

A for loop repeats code a set number of times by:

  • Initializing a loop variable.

  • Checking a condition before each iteration.

  • Updating the variable after each iteration.

Example:

basic counter for loop

Explanation:

Explanation of sections of for loop

How it Works

Loop variable: int i = 0 → starts the counter.

Condition: i < 11 → loop runs while true (will run 10 times).

Increment: i++ → increases the counter after each iteration.

Now you could update the condition if you know the amount of times you wish to run to

for(int i = 0; i<=10; i++){
   // do something
}
Enter fullscreen mode Exit fullscreen mode

Handy Loop Operators

i++ → shorthand for i = i + 1.

i-- → decrease by 1.

i += n or i -= n → increase or decrease by more than 1.

Example (count in 3s):

for (int i = 0; i < 10; i += 3)
{
    Console.WriteLine(i);
}
Enter fullscreen mode Exit fullscreen mode

Real-World Use of Loops

In practice, loops are most often used to:

Iterate through an array (numbers, words, etc.).

Iterate through a collection or list of objects and work with their data.

Example: Address Book

using System;
using System.Collections.Generic;

public class Address
{
    public string Name { get; set; }
    public string AddressLineOne { get; set; }
    public int HouseNumber { get; set; }
    public string PostCode { get; set; }
    public string Telephone { get; set; }
}

public class Program
{
    public static void Main()
    {
        var addressBook = new List<Address>
        {
            new Address { Name = "Grant", AddressLineOne = "Developer Avenue", HouseNumber = 1, PostCode = "DV19 8EP", Telephone = "0102919 93020-92019" },
            new Address { Name = "Bill", AddressLineOne = "Developer Avenue", HouseNumber = 19, PostCode = "DV19 8EP", Telephone = "0102919 93020-92019" },
            new Address { Name = "Rebecca", AddressLineOne = "Developer Avenue", HouseNumber = 4, PostCode = "DV19 8EP", Telephone = "0102919 93020-92019" },
            new Address { Name = "Amy", AddressLineOne = "Rower Avenue", HouseNumber = 1, PostCode = "DV19 8EP", Telephone = "0102919 93020-92019" },
            new Address { Name = "Joe", AddressLineOne = "Olympic Drive", HouseNumber = 1, PostCode = "DV19 10E", Telephone = "0102919 93020-92019" }
        };

        for (var i = 0; i < addressBook.Count; i++)
        {
            Console.WriteLine(addressBook[i].Name);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

What’s Happening?

We defined a simple Address class.

We created a list of Address objects using List<Address>.

The <Address> part tells C# this list only holds Address objects.

The for loop runs addressBook.Count times (5 in this case).

Loop starts at i = 0.

Condition: i < addressBook.Count.

Each iteration, i increases by 1 (i++).

Inside the loop, we use the index accessor (addressBook[i]) to get each object and print its Name.

Loops and Indexes

Indexes in C# start at 0 (zero-based).

That means:

First item → index 0

Second item → index 1

*Third item *→ index 2

…and so on.

So in our loop, i works as the index for each item:

var name = addressBook[i].Name;
Enter fullscreen mode Exit fullscreen mode

This is why the for loop is so powerful for lists — the loop variable (i) lines up perfectly with the index of each item.

👉 Try changing which property you print (e.g., PostCode or Telephone) to see different output.

Next up → Foreach Loop (an even easier way to loop through collections).

Top comments (0)