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 improved our website User Interface and other contents. Our next update is adding video tutorials on this site so, We create a new YouTube channel, our YouTube channel name is CodeX.

We need some time for make video tutorials (CodeX) so, stay with us and keep supporting and you know already we make a Collaboration with HackIsOn YouTube channel long time ago So, please Subscribe Our Channels