C# の基礎

Loops and Iteration

C# offers several loop forms so iteration can express either indexes, values, or a changing condition.

How it works

Use for, foreach, and while loops safely.

foreach avoids manual indexes when only values are needed. continue skips the current item without ending the loop.

Runnable C# example

C#
Run code →
Program.cs
using System;

class Program
{
    static void Main()
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6 };
        foreach (int number in numbers)
        {
            if (number % 2 != 0) continue;
            Console.WriteLine(number * number);
        }
    }
}
Expected output
4
16
36

Practice with the debugger

Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.