Loading...

Loops: For, While and Do-While

Imagine playing a game where you have to clap every time the number increases. If you were to clap 10 times, would you do it one by one manually, or ask someone (or something!) to repeat it for you?

Computers love repeating things.
They can do the same action 10, 100, or even 1,000,000 times without getting tired.
This superpower comes from loops.

Today, you’ll learn how to make C# repeat tasks automatically, and you’ll build your first number-game program.

Multiplication Table & Number Games

You’ll write a program that prints a multiplication table, for example, for the number 5:

text
5 lines
|
14/ 500 tokens
1
2
3
4
5
5 x 1 = 5  
5 x 2 = 10  
5 x 3 = 15  
...  
5 x 10 = 50

Then you’ll add a fun number game that prints all numbers from 1 to 20, skipping 10, and stopping at 15.

This will teach you looping + break + continue.

Why Loops Matter

Imagine writing:

text
4 lines
|
18/ 500 tokens
1
2
3
4
Console.WriteLine(1);
Console.WriteLine(2);
Console.WriteLine(3);
...

It would take forever, and look silly.

Loops make your programs:

  • shorter
  • faster
  • smarter
  • more dynamic

Now let’s meet the 3 loops that power almost every app you’ll ever build.

The For Loop (Best When You Know the Count)

Use a for loop when you know how many times you want to repeat an action.

Example:

csharp
4 lines
|
15/ 500 tokens
1
2
3
4
for (int i = 1; i <= 5; i++)
{
    Console.WriteLine(i);
}
Code Tools

This prints 1 to 5.

The While Loop (Best for Repeating Until Something Happens)

Use while when you don’t know how many times you’ll repeat, but you know the condition.

Example:

csharp
7 lines
|
21/ 500 tokens
1
2
3
4
5
6
7
int count = 1;

while (count <= 5)
{
    Console.WriteLine(count);
    count++;
}
Code Tools

Same output, different purpose.

The Do-While Loop (Runs At Least Once)

do-while always runs the block before checking the condition.

Example:

csharp
8 lines
|
18/ 500 tokens
1
2
3
4
5
6
7
8
int x = 1;

do
{
    Console.WriteLine(x);
    x++;
}
while (x <= 5);
Code Tools

Even if the condition is false, it will run once, great for menus and retry screens.

Counting Up

csharp
1 lines
|
8/ 500 tokens
1
for (int i = 1; i <= 10; i++)
Code Tools

Counting Down

csharp
1 lines
|
8/ 500 tokens
1
for (int i = 10; i >= 1; i--)
Code Tools

Skipping Values (continue)

csharp
1 lines
|
6/ 500 tokens
1
if (i == 5) continue;
Code Tools

Stopping Early (break)

csharp
1 lines
|
5/ 500 tokens
1
if (i == 8) break;
Code Tools

These patterns appear everywhere, from games to billing apps.

break

Stops the loop immediately.

csharp
2 lines
|
6/ 500 tokens
1
2
if (i == 10)
    break;
Code Tools

continue

Skips the current iteration and moves to the next one.

csharp
2 lines
|
7/ 500 tokens
1
2
if (i == 5)
    continue;
Code Tools

You’ll use both in your number game.

Let’s Build the Multiplication Table Program

csharp
17 lines
|
89/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter a number:");
        int num = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine($"Multiplication Table of {num}");
        
        for (int i = 1; i <= 10; i++)
        {
            Console.WriteLine($"{num} x {i} = {num * i}");
        }
    }
}
Code Tools

Simple, clean, and powerful.

Now Add the Number Game

csharp
12 lines
|
48/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
Console.WriteLine("\nNumber Game:");

for (int i = 1; i <= 20; i++)
{
    if (i == 10)
        continue; // skip 10

    if (i == 15)
        break; // stop at 15

    Console.WriteLine(i);
}
Code Tools

This teaches true loop control, a must-have skill for all programmers.

How It Works

  • for loop repeats the multiplication table steps
  • continue skips printing number 10
  • break stops the loop at 15
  • Logic + repetition = smart programs

Run it in the DevsCall AI Online Runner to watch it work instantly.

Learn Together with AI

Try prompts like:

  • “Create a loop that prints even numbers only.”
  • “Rewrite this using while instead of for.”
  • “Explain break vs continue in simpler words.”
  • “Make a version that prints tables up to 20.”

AI becomes your personal loop tutor.

Practice Time

Write a program that:

  1. Asks the user for a number
  2. Prints all numbers from 1 to that number
  3. Skips numbers divisible by 3
  4. Stops completely if it reaches a number > 50

Expected output example:

text
8 lines
|
13/ 500 tokens
1
2
3
4
5
6
7
8
1
2
4
5
7
8
...
Stopped because number exceeded 50.

Example Solution

csharp
24 lines
|
118/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter a limit:");
        int limit = Convert.ToInt32(Console.ReadLine());

        for (int i = 1; i <= limit; i++)
        {
            if (i > 50)
            {
                Console.WriteLine("Stopped because number exceeded 50.");
                break;
            }

            if (i % 3 == 0)
                continue;

            Console.WriteLine(i);
        }
    }
}
Code Tools

If your output matches, you’ve mastered loops!

Frequently Asked Questions

A loop repeats a block of code automatically, making tasks faster and reducing repeated lines of code.

Use a for loop when you know exactly how many times you want to repeat an action, such as printing numbers 1–10.

Use a while loop when you want to repeat something as long as a condition remains true, even if you don’t know how many times.

break stops the loop immediately, even if the ending condition hasn’t been reached.

continue skips the current iteration and moves directly to the next repetition.

Loop patterns are common uses like counting up, counting down, skipping numbers, or stopping early.

Yes. One loop can run inside another, which is useful for printing tables or working with multi-dimensional data.

Still have questions?Contact our support team