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);
4Print Transaction Numbers Using Loops
Let's use all three loops to display transaction numbers from 1 to 3.
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
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.
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
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.
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.
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.
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.
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.