0Pricing
C# Academy · Lesson

Loops in C#: For, While, and Do-While

Master looping structures to perform repetitive tasks efficiently.

Loops in C#: For, While, and Do-While is a free C# Academy lesson on CoddyKit — lesson 2 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Loops in C#: For, While, and Do-While

Welcome to the next lesson! In this lesson, you’ll learn how to use loops to repeat tasks in your C# programs, making your code more efficient. Let’s get started!

Loops in C#: For, While, and Do-While — illustration 1

2

What Are Loops?

Loops allow you to repeat a block of code multiple times. They are useful for automating repetitive tasks like iterating over data or performing calculations.

Key Point: C# provides three main types of loops: for, while, and do-while.

3

The For Loop

The for loop is used when you know how many times you want to repeat a task. Example:

Key Point: The for loop consists of three parts: initialization, condition, and increment.

using System;

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

4

The While Loop

The while loop is used when you want to repeat a task as long as a condition is true. Example:

Tip: Make sure the condition eventually becomes false to avoid infinite loops.

using System;

class Program {
    static void Main() {
        int i = 1;

        while (i <= 5) {
            Console.WriteLine("Iteration: " + i);
            i++; // Increment the counter
        }
    }
}

5

The Do-While Loop

The do-while loop is similar to the while loop but guarantees the code runs at least once. Example:

Key Point: Use do-while when the task needs to execute at least once, regardless of the condition.

using System;

class Program {
    static void Main() {
        int i = 1;

        do {
            Console.WriteLine("Iteration: " + i);
            i++;
        } while (i <= 5);
    }
}

6

Breaking Out of Loops

Use the break statement to exit a loop prematurely. Example:

Tip: Use break sparingly to keep your code easy to follow.

using System;

class Program {
    static void Main() {
        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                break; // Exit the loop when i equals 5
            }
            Console.WriteLine("Iteration: " + i);
        }
    }
}

7

Skipping Iterations

Use the continue statement to skip the rest of the current iteration and move to the next one. Example:

Key Point: Use continue to skip specific iterations while staying in the loop.

using System;

class Program {
    static void Main() {
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                continue; // Skip even numbers
            }
            Console.WriteLine("Odd number: " + i);
        }
    }
}

8

Nested Loops

You can nest loops inside each other for more complex tasks. Example:

Tip: Be cautious with nested loops as they can increase the complexity of your program.

using System;

class Program {
    static void Main() {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 2; j++) {
                Console.WriteLine("Outer loop: " + i + ", Inner loop: " + j);
            }
        }
    }
}

10

Great Job!

Congratulations! You’ve learned how to use for, while, and do-while loops to repeat tasks efficiently. In the next lesson, we’ll explore switch statements to simplify complex conditional logic. Let’s keep coding!

Loops in C#: For, While, and Do-While — illustration 9

Frequently asked questions

Is the “Loops in C#: For, While, and Do-While” lesson free?

Yes — the full text of “Loops in C#: For, While, and Do-While” is free to read here on the web, and the C# Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Loops in C#: For, While, and Do-While”?

Master looping structures to perform repetitive tasks efficiently. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Loops in C#: For, While, and Do-While” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Control Flow: If-Else Statements
  2. Loops in C#: For, While, and Do-While
  3. Switch Statements
  4. Introduction to Arrays
  5. Working with Strings
← Back to C# Academy