WeCodeAcademy

C# Loops

ExampleC#
using System;

class Program
{
    static void Main()
    {
        for (int i = 1; i <= 3; i++)
        {
            Console.WriteLine("for " + i);
        }

        int n = 3;
        while (n > 0)
        {
            Console.WriteLine("while " + n);
            n--;
        }

        string[] cars = { "Volvo", "BMW", "Ford" };
        foreach (string car in cars)
        {
            Console.WriteLine(car);
        }

        for (int i = 0; i < 10; i++)
        {
            if (i == 2) continue;
            if (i == 4) break;
            Console.WriteLine("i=" + i);
        }
    }
}
Output
for 1
for 2
for 3
while 3
while 2
while 1
Volvo
BMW
Ford
i=0
i=1
i=3
Quick check

Which loop walks through every item of an array?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.