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 = 0
sets up the loop variablei
to start at 0. - Condition:
i < 5
specifies that the loop should continue as long asi
is less than 5. - Iteration:
i++
incrementsi
by 1 after each loop iteration. - The code inside the loop prints the value of
i
each 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
j
is less than 5. - The code inside the loop prints the value of
j
and then incrementsj
by 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
do
block executes first before checking the condition. - The loop continues to run as long as
k
is 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
for
loop runs 3 times. - For each iteration of the outer loop, the inner
for
loop runs 3 times. - This results in a total of 9 iterations, printing all combinations of
i
andj
.
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
for
loop,break
exits the loop wheni
equals 5, so only values from 0 to 4 are printed. - In the second
for
loop,continue
skips 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
for
loop iterates over each element of thefruits
array 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
while
loop adds numbers from 1 to 10 to thesum
variable. - 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