C# Loops with Examples | for, while, and do-while Loop in C#

Loops in C# or in any other programming language are used when we need to execute some block of code for the required number of times. Or we can use a loop to continue executing some defined block of code until the specified condition evaluates to false.

We are going to discuss three types of loops, which are:

C# while loop

The general form of the while loop in C# is:

while (condition)
{
   // block of code to continue
   // its execution until the 
   // condition evaluates to be false
}

For example

Console.WriteLine("Enter a Number: ");
int num = Convert.ToInt32(Console.ReadLine());

int i = 1;

Console.WriteLine("\nMultiplication Table of {0}", num);
while (i <= 10)
{
    Console.WriteLine(num * i);
    i++;
}

The sample run with user input 3 should exactly be as shown in the snapshot given below.

c sharp while loop example

The dry run of above C# program should be:

C# do...while loop

The do...while loop works similar to the while loop, but instead the block of code inside the loop will be executed at once, even if the defined condition is evaluated to be false at first. Here is the general form of the do...while loop in C#:

do
{
   // block of code to be executed
   // at first, and then continue 
   // its execution until the 'condition'
   // evaluates to be false
} while (condition);

For example:

int num = 10;
do
{
    Console.WriteLine("Inside the do...while loop");
} while (num < 0);

As you can see from this C# example, the condition num < 0 or 10 < 0 is false at first evaluation. But still, the block of code available inside the do...while loop will be executed. Therefore, the output should exactly be:

Inside the do...while loop

Now let me create another example that does the same job as the example used in the while loop:

Console.WriteLine("Enter a Number: ");
int num = Convert.ToInt32(Console.ReadLine());

int i = 1;

Console.WriteLine("\nMultiplication Table of {0}", num);
do
{
    Console.WriteLine(num * i);
    i++;
} while (i <= 10);

Now, with user input number 7, the sample run of this C# example to show how the do...while loop works should be:

Enter a Number:
7

Multiplication Table of 7
7
14
21
28
35
42
49
56
63
70

C# for loop

Unlike the while and do...while loops, the for loop contains initialization, condition checking, and an update statement all in one place. Here is the general form of the for loop in C#:

for (initialize; condition; update)
{
   // block of code to be executed
   // until the "condition" evaluates 
   // to be false
}

The initialize statement will be executed once, at the start of the loop. The condition statement will be executed every time before entering the loop. And after executing all the blocks of code defined inside the loop, the update statement will be executed. For example:

Console.WriteLine("Enter a Number: ");
int num = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("\nMultiplication Table of {0}", num);
for (int i=1;  i <= 10; i++)
    Console.WriteLine(num * i);

With user input 13, the sample run of this C# example is shown in the snapshot given below.

c sharp for loop example

C# foreach loop

The foreach loop is used when we need to loop through each element of a specified array. Here is the general form of the foreach loop in C#.

foreach (type x in array) 
{
   // block of code to be executed
   // until the last element of the 
   // array
}

For example:

string[] cities = { "Boston", "Dallas", "Houston", "El Paso", "Phoenix" };
foreach(string x in cities)
{
    Console.WriteLine(x);
}

The output should exactly be:

Boston
Dallas
Houston
El Paso
Phoenix

C# Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!