Welcome to Lesson 7! In this lesson, we’ll explore loops in C#, which are fundamental for repeating code blocks multiple times. Loops help you handle repetitive tasks efficiently and are essential for many programming tasks. We’ll cover three types of loops in C#: for, while, and do-while.
1. The for Loop
The for loop is used when you know in advance how many times you want to execute a block of code. It consists of three parts: initialization, condition, and iteration.
Syntax:
for (initialization; condition; iteration)
{
// Code to execute each time the loop runs
}
Example:
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Iteration: " + i);
}
Explanation:
- Initialization:
int i = 0sets up the loop variableito start at 0. - Condition:
i < 5specifies that the loop should continue as long asiis less than 5. - Iteration:
i++incrementsiby 1 after each loop iteration. - The code inside the loop prints the value of
ieach time the loop runs.
2. The while Loop
The while loop is used when you want to repeat a block of code as long as a specified condition is true. It’s suitable when you don’t know in advance how many times the loop will need to run.
Syntax:
while (condition)
{
// Code to execute as long as the condition is true
}
Example:
int j = 0;
while (j < 5)
{
Console.WriteLine("Iteration: " + j);
j++;
}
Explanation:
- The loop continues as long as
jis less than 5. - The code inside the loop prints the value of
jand then incrementsjby 1.
3. The do-while Loop
The do-while loop is similar to the while loop, but it guarantees that the loop’s code block is executed at least once, even if the condition is false initially.
Syntax:
do
{
// Code to execute at least once, and then repeat while the condition is true
}
while (condition);
Example:
int k = 0;
do
{
Console.WriteLine("Iteration: " + k);
k++;
}
while (k < 5);
Explanation:
- The
doblock executes first before checking the condition. - The loop continues to run as long as
kis less than 5.
4. Nested Loops
You can nest loops within other loops to handle more complex scenarios, such as iterating through multi-dimensional data.
Example:
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("i: " + i + ", j: " + j);
}
}
Explanation:
- The outer
forloop runs 3 times. - For each iteration of the outer loop, the inner
forloop runs 3 times. - This results in a total of 9 iterations, printing all combinations of
iandj.
5. Using break and continue
break: Exits the loop immediately, regardless of the loop condition.continue: Skips the current iteration and proceeds to the next iteration of the loop.
Example:
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break; // Exit the loop when i equals 5
}
Console.WriteLine("i: " + i);
}
for (int j = 0; j < 10; j++)
{
if (j % 2 == 0)
{
continue; // Skip even numbers
}
Console.WriteLine("j: " + j);
}
Explanation:
- In the first
forloop,breakexits the loop wheniequals 5, so only values from 0 to 4 are printed. - In the second
forloop,continueskips even numbers, printing only odd values from 1 to 9.
6. Example: Looping Through an Array
Loops are often used to iterate through arrays and collections.
Example:
string[] fruits = { "Apple", "Banana", "Cherry" };
for (int i = 0; i < fruits.Length; i++)
{
Console.WriteLine("Fruit: " + fruits[i]);
}
Explanation:
- The
forloop iterates over each element of thefruitsarray and prints it.
7. Example: Sum of Numbers
Let’s use a while loop to calculate the sum of numbers from 1 to 10.
Example:
int sum = 0;
int number = 1;
while (number <= 10)
{
sum += number;
number++;
}
Console.WriteLine("Sum of numbers from 1 to 10: " + sum);
Explanation:
- The
whileloop adds numbers from 1 to 10 to thesumvariable. - After the loop completes, it prints the total sum.
Summary
In this lesson, you learned about the three main types of loops in C#: for, while, and do-while. We covered their syntax, use cases, and how to use them to handle repetitive tasks in your code. Additionally, we explored nested loops, and the break and continue statements to control loop execution more precisely.
Happy coding!

0 Comments