In this tutorial, we will learn about while and do…while loop in C#, how to use them and difference between them.

Loops can execute a block of code up to a predetermined condition is reached.

Loops are handy on the grounds that they save time, reduce errors, and they make code more readable.

In programming, it is often desired to execute certain block of statements for a predefined number of times. A possible arrangement will be to type those statements for the necessary number of times. In any case, the number of repetition may not be known ahead of time (during compile time) or possibly enormous enough (say 10000).

The best solution for such issue is loop. Loops are used in programming to over and again execute a specific block of statements until some condition is met.

In this tutorial, we’ll learn to use while loops in C#.


C# while loop

The while catchphrase is used to create while loop in C#. The syntax for while circle is:

while (test-expression)
{
	// body of while
}

How while loop works?

  1. C# while loop comprises of a test-expression.
  2. If the test-expression is assessed to true,

a. statements inside the while loop are executed.

b. after execution, the test-expression is evaluated again.

  1. If the test-expression is evaluated to false, the while loop ends.

while loop Flowchart

Example 1: while Loop

using System;

namespace Loop
{
	class WhileLoop
	{
		public static void Main(string[] args)
		{
			int i=1;
			while (i<=5)
			{
				Console.WriteLine("C# For Loop: Iteration {0}", i);
				i++;
			}
		}
	}
}

At the point when we run the program, the output will be:

C# For Loop: Iteration 1
C# For Loop: Iteration 2
C# For Loop: Iteration 3
C# For Loop: Iteration 4
C# For Loop: Iteration 5

At first the value of I is 1.

At the point when the program comes to the while loop statements,

• the test expression I <=5 is assessed. Since I is 1 and 1 <= 5 is true, it executes the body of the while loop. Here, the line is printed on the screen with Iteration 1, and the value of I is increased by 1 to become 2.

• Now, the test expression (I <=5) is assessed once more. This time as well, the expression returns true (2 <= 5), so the line is printed on the screen and the value of I is presently augmented to 3..

• This goes and the while loop executes until I becomes 6. Now, the test-expression will assess to false and thus the loop ends.

Example 2: while loop to compute sum of first 5 natural numbers

using System;

namespace Loop
{
	class WhileLoop
	{
		public static void Main(string[] args)
		{
			int i=1, sum=0;

			while (i<=5)
			{
				sum += i;
				i++;
			}
			Console.WriteLine("Sum = {0}", sum);
		}
	}
}

At the point when we run the program, the output will be:

Sum = 15

This program computes the sum of first 5 natural numbers.

• Initially the value of sum is initialized to 0.

• On each emphasis, the value of sum is updated to sum+i and the value of I is increased by 1.

• When the value of I arrives at 6, the test expression i<=5 will return false and the loop ends.

Let’s see what happens in the given program on each iteration.

Initially, i = 1, sum = 0

While loop execution steps

IterationValue of ii<=5Value of sum
11true0+1 = 1
22true1+2 = 3
33true3+3 = 6
44true6+4 = 10
55true10+5 = 15
66falseLoop terminates

In this way, the last value of sum will be 15.


C# do…while loop

The do and while keyword is used to create a do…while loop. It is like some while loop, anyway there is a significant distinction between them.

In while loop, the condition is checked before the body is executed. It is the specific inverse in do…while loop, for example condition is checked after the body is executed.

This is the reason, the body of do…while loop will execute in any event once irrespective to the test-expression.

The syntax for do…while loop is:

do
{
	// body of do while loop
} while (test-expression);

How do…while loop works?

  1. The body of do…while loop is executed from the outset.
  2. Then the test-expression is assessed.
  3. If the test-expression is true, the body of loop is executed.
  4. When the test-expression is false, do…while loop ends.

do…while loop Flowchart

Example 3: do…while loop

using System;

namespace Loop
{
	class DoWhileLoop
	{
		public static void Main(string[] args)
		{
			int i = 1, n = 5, product;

			do
			{
				product = n * i;
				Console.WriteLine("{0} * {1} = {2}", n, i, product);
				i++;
			} while (i <= 10);
		}
	}
}

At the point when we run the program, the output will be:

5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

As should be obvious, the above program prints the multiplication table of a number (5).

• Initially, the value of I is 1. The program, at that point enters the body of do..while loop without checking any condition (rather than while loop).

• Inside the body, item is calculated and printed on the screen. The value of I is then increased to 2.

• After the execution of the loop’s body, the test expression I <= 10 is assessed. Altogether, the do…while loo will run for 10 times.

• Finally, when the value of I is 11, the test-expression assesses to false and henceforth ends the loop.


Infinite while and do…while loop

In the event that the test expression in the while and do…while loop never assesses to false, the body of loop will run forever. Such loops are called infinite loop.

For instance:

Infinite while loop

while (true)
{
	// body of while loop
}

Infinite do…while loop

do
{
	// body of while loop
} while (true);

The infinite loop is useful when we need a loop to run as long as our program runs.

For instance, if your program is an animation, you should continually run it until it is stopped. In such cases, an infinite loop is important to continue to run the animation over and again.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.


0 Comments

Leave a Reply