C# do while loop with examples

In the previous lessons, we learned about C# for loop and C# while loop. In this tutorial, we are going to learn C# do-while loop. The for loop and while checks the condition at beginning of loop then execute the statement.

 

C# do-while loop -


The do-while loop executes the statement at least one time and checks the condition end of the loop.

In simple words, In the for loop and while loop compiler checks the condition first and execute the block of code but in the do-while loop, compiler executes the block of code once and check condition at the end of the block.

 

The C# while loop and C# do-while loop can say opposite each other. In the while loop, we write condition first then block of code but in the C# do-while loop, we write only do and block code. Condition writes the end of the block.

Let's understand the C# do-while loop with syntax.

Syntax of C# do-while loop.

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

In the above syntax, you can see that the code of block is executed once then checks the condition. In the while loop, while condition comes before the block of code but in the do-while loop, while condition comes after the code of block end.

 

There is no programming without practice. Let's create an example for the do-while loop in C# programming.

 

Example of C# do-while loop -


In this example,we are going to printing increasing values of a variable using C# do-while loop.


using System;

namespace DowhileLoop{

   public class Program {
   
      public static void Main(string[] args) {
		 int i=0; 
		  int lp=0; 
		  do{ 
             i++;
            lp++; 			  
    Console.WriteLine("Loop "+lp+", Value of i: "+i);
   
		  } 
		  while(i<17);  
   }
}
}

Output - 

Loop 1, Value of i: 1
Loop 2, Value of i: 2
Loop 3, Value of i: 3
Loop 4, Value of i: 4
Loop 5, Value of i: 5
Loop 6, Value of i: 6
Loop 7, Value of i: 7
Loop 8, Value of i: 8
Loop 9, Value of i: 9
Loop 10, Value of i: 10
Loop 11, Value of i: 11
Loop 12, Value of i: 12
Loop 13, Value of i: 13
Loop 14, Value of i: 14
Loop 15, Value of i: 15
Loop 16, Value of i: 16
Loop 17, Value of i: 17
 

In the above C# example, we declared two variables with value(Initial value).In do-while, we first write do keyword-only then create a block of code. We make a condition at the end of do block using while keyword.
In the above example, we used ++ increment operator to increment variables value by 1 in every loop.


If else and break statement with C# do-while loop.


In the programming world, we create unique code and effective to run smoothly. In this C# example, we are going to use if-else and break statement with a do-while loop. It helps to understand making effective codes using control structures and loops in C# programming.


using System;

namespace DowhileLoop{

   public class Program {
   
      public static void Main(string[] args) {
		 int a=1;
do {
	
    if (a < 5) {
   
			Console.WriteLine("a is not big enough");
        break;
    }
	else {
		
		Console.WriteLine("a is big enough");
        break;
	}
}

 while (0>a);
		  
		 
}
}
}

 

Output -

a is not big enough

In the above example, we wrote if-else and break inside the do block and execute. In this way, you can use another control structure and loop in C# programming.