C# While Loops :

while loop :

One of the simplest and most commonly used loops is while.

Syntax :

while (condition)
{
loop body;
}


In the code above example, condition is any expression that returns a Boolean resulttrue or false. It determines how long the loop body will be repeated and is called the loop condition. In this example the loop body is the programming code executed at each iteration of the loop, i.e. whenever the input condition is true.


Example :

  1. /* Description: Summing the Numbers from 1 to 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. int num = 1;
  12. int sum = 1;
  13. Console.Write("The sum 1");
  14. while (num < n)
  15. {
  16. num++;
  17. sum += num;
  18. Console.Write(" + " + num);
  19. }
  20. Console.WriteLine(" = " + sum);
  21. Console.ReadKey();
  22. }
  23. }
  24. }

Output :

C Sharp Language Summing the Numbers from 1 to N

Next Update

Loading...

Currently, we are working on other servers, but soon we update our website with new content. So, stay tuned we are back soon with new content.

Free Courses