Lesson 6 of 15

Loops

Loops

C# has three main loop types:

for Loop

for (int i = 1; i <= 5; i++) {
    Console.WriteLine(i);
}
// Prints 1, 2, 3, 4, 5

while Loop

int n = 1;
while (n <= 3) {
    Console.WriteLine(n);
    n++;
}

foreach Loop

Iterates over a collection:

string[] fruits = { "apple", "banana", "cherry" };
foreach (string fruit in fruits) {
    Console.WriteLine(fruit);
}

Your Task

Use a for loop to print the first 5 multiples of 3 (3, 6, 9, 12, 15).

WasmSharp (.NET) loading...
Loading...
Click "Run" to execute your code.