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...

We Are Currently Working On Our Android Application Development and We already 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, Thank you