← C# from Scratch
Lesson 18 of 39 · 10 XP
The for loop
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
for (start; condition; step) — when the number of repetitions is known:
for (int i = 1; i <= 5; i++)
Console.Write(i + " "); // 1 2 3 4 5
for (int i = 10; i > 0; i -= 3)
Console.Write(i + " "); // 10 7 4 1
foreach goes through the items of a collection or the characters of a string:
foreach (char c in "IGD")
Console.WriteLine(c);
Loops can be nested: the outer one for rows, the inner one for columns.
Task. Read n and print an n × n multiplication table: row i contains the numbers i*1 … i*n separated by spaces.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.