C# Do-While Loops :

do-while loop :

The do-while loop is similar to the while loop, but it checks the condition after each execution of its loop body. This type of loops is called loops with condition at the end (post-test loop).

The do-while loop is used when we want to guarantee that the sequence of operations in it will be executed repeatedly and at least once in the beginning of the loop. A do-while loop looks like this:

Syntax :

do{
executable code;
}
while (condition);


Example :

  1. /* Description: calculate the factorial of a given number N */
  2. using System;
  3. namespace DemoProgramming
  4. {
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. Console.Write("n = ");
  10. int n = int.Parse(Console.ReadLine());
  11. decimal factorial = 1;
  12. do
  13. {
  14. factorial *= n;
  15. n--;
  16. } while (n > 0);
  17. Console.WriteLine("n! = " + factorial);
  18. Console.ReadKey();
  19. }
  20. }
  21. }

Output :

C Sharp Language calculate the factorial of a given number N

Next Update

Loading...

We have two YouTube channels their name is CodeX or NightHawk. CodeX is for programming related videos and NightHawk is for online game streaming so please supporting us and Subscribe to our channels