C# Conditional Loops

In Previous article we learned about C# Conditional Statements and now in this article we will learn about C# Conditional Loops using various examples.

In C#, conditional loops are used to repeatedly execute a block of code as long as a specific condition is true. Here are some of the most commonly used conditional loops:

1.while loop: The while loop is used to execute a block of code repeatedly while a condition is true. The syntax is as follows:

while (condition)
{
    // code to be executed
}

Here, is the example of while loop,

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

This code will repeatedly print the value of i as long as it is less than or equal to 10. The output will be:

1
2
3
4
5
6
7
8
9
10

2.do-while loop: The do-while loop is similar to the while loop, but the condition is checked at the end of the loop instead of the beginning. This ensures that the loop body is executed at least once. The syntax is as follows:

do
{
    // code to be executed
} while (condition);

Here is the example of do-while loop,

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

This code will execute the block of code at least once and then repeatedly execute it as long as the condition is true. The output will be the same as the previous example:

1
2
3
4
5
6
7
8
9
10

Note that the difference between the while and do-while loops is that the while loop tests the condition at the beginning, while the do-while loop tests the condition at the end. Therefore, the block of code inside a do-while loop will always execute at least once, even if the condition is initially false.

3.for loop: The for loop is used to execute a block of code a specific number of times. It is often used when the number of iterations is known in advance. The syntax is as follows:

for (initialization; condition; iteration)
{
    // code to be executed
}

here’s an example of a for loop in C#:

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

In this example, the loop will execute 5 times because the condition i < 5 is true. The loop consists of three parts separated by semicolons:

  1. Initialization: int i = 0; initializes the loop control variable i to 0.
  2. Condition: i < 5; checks whether the loop should continue executing. As long as the condition is true, the loop will execute.
  3. Iteration: i++ increments the value of i by 1 after each iteration of the loop.

The output of this code will be:

0
1
2
3
4

In this example, the loop control variable i is used to print the values from 0 to 4 to the console. However, the loop control variable can be used for other purposes as well, such as accessing elements in an array or collection.

4.foreach loop: The foreach loop is used to iterate over the elements of an array or a collection. It automatically initializes an index variable and iterates over the elements until the end of the collection is reached. The syntax is as follows:

foreach (type variable in collection)
{
    // code to be executed
}

here’s an example of a foreach loop in C#:

int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
    Console.WriteLine(number);
}

In this example, the loop will iterate over each element in the numbers array and print its value to the console. The syntax of the foreach loop is as follows:

foreach (type variable in collection)
{
    // code to be executed
}

Here, type specifies the data type of the elements in the collection, variable is a temporary variable that represents each element in the collection, and collection is the collection of elements to be iterated over. The loop will automatically initialize the variable with each element in the collection and execute the code inside the loop body.

The output of this code will be:

1
2
3
4
5

In this example, the loop iterates over each element in the numbers array and prints its value to the console. The loop control variable number is used to access each element in the array. However, the loop control variable can be named whatever you like, as long as it is of the same type as the elements in the collection.

RSS
Follow by Email
YouTube
YouTube
LinkedIn
Share