LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
C# Programming

Loops in C# – For, While and Do While

A loop executes the same block of code repeatedly while a condition is satisfied. Without loops, we would need to write the same statement many times. Loops are useful for processing records, displaying lists, calculating totals and reading data. C# provides different loop structures, including for, while and do-while.

1Why Do We Need Loops?

Hi friends, in this article we will learn about loops in C#. A loop executes the same block of code repeatedly while a condition is satisfied. Without loops, we would need to write the same statement many times. Loops are useful for processing records, displaying lists, calculating totals and reading data. C# provides different loop structures, including for, while and do-while.

2Think About a Bank Account

Imagine a banking application that needs to display the last five transactions. Writing five separate print statements is possible, but what happens when there are 500 transactions?

A loop can repeat the same operation for every transaction. It starts, checks a condition, executes the code and continues until the condition becomes false. This saves code and makes the application easier to maintain.

3Explore the Three Main Loops

1. For loop

Use a for loop when you know how many times the code should execute. It contains initialization, condition and update.

for (int i = 1; i <= 5; i++)
{
    Console.WriteLine(i);
}

2. While loop

A while loop checks its condition before executing the code. It is useful when the number of repetitions is not known in advance.

int i = 1;
while (i <= 5)
{
    Console.WriteLine(i);
    i++;
}

3. Do-while loop

A do-while loop executes its code first and checks the condition afterwards. Therefore, it always executes at least once.

int i = 1;
do
{
    Console.WriteLine(i);
    i++;
} while (i <= 5);
Remember A for and while loop may execute zero times. A do-while loop executes at least once.
Pro Tip Use a for loop for a known count, and while for a condition-driven repetition.
Common Mistake Forgetting to update the loop variable can create an infinite loop.

4Print Transaction Numbers Using Loops

Let's use all three loops to display transaction numbers from 1 to 3.

Program.csC#
using System;

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

        Console.WriteLine("While Loop:");
        int j = 1;
        while (j <= 3)
        {
            Console.WriteLine(j);
            j++;
        }

        Console.WriteLine("Do While Loop:");
        int k = 1;
        do
        {
            Console.WriteLine(k);
            k++;
        } while (k <= 3);
    }
}

Each loop starts at 1, prints the current number and increments it. When the value becomes 4, the condition is false and the loop stops.

5Check the Program Result

Output
For Loop:
1
2
3
While Loop:
1
2
3
Do While Loop:
1
2
3

All three loops print the numbers 1, 2 and 3. The difference is when each loop checks its condition.

6Watch Each Loop Execute in VS Code

Follow the counter as each loop prints its values and moves to the next section.

Program.cs - Visual Studio Code
▤⌕⑂⚙
● Program.cs
 
PROBLEMSOUTPUTDEBUG CONSOLETERMINAL
 
Ln 1, Col 1  UTF-8  C#

7Keep These Loop Rules Clear

Loops reduce repeated code and make data processing easier. Choose the loop based on when you want to check the condition and whether you know the repetition count.

  • For: Best when the count is known.
  • While: Checks the condition before execution.
  • Do-while: Executes once before checking.

8Questions Interviewers May Ask

Q1Easy

What is a loop in C#?

AnswerA loop repeatedly executes a block of code while its condition allows it. It avoids writing the same statements multiple times.

Q2Easy

What is the difference between for and while?

AnswerA for loop groups initialization, condition and update in one statement. A while loop checks a condition and usually updates its variable inside the loop body.

Q3Medium

Why does do-while execute at least once?

AnswerBecause its condition is checked after the loop body. The statements execute before the first condition check.

Q4Medium

What is an infinite loop?

AnswerAn infinite loop continues without stopping because its condition never becomes false. Missing an increment or update is a common cause.

Q5Hard

What is the difference between break and continue?

Answerbreak immediately exits the loop. continue skips the remaining statements in the current iteration and moves to the next iteration.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.