C# For Loops :

for-loop :

For-loops are a slightly more complicated than while and do-while loops but on the other hand they can solve more complicated tasks with less code. Here is the describing for-loops syntax and example:

Syntax :

for (initialization; condition; update)
{
loop's body;
}


Example :

  1. /* Description: Basic for-loop 1 to 10 number show example */
  2. using System;
  3. namespace DemoProgramming
  4. {
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. int i;
  10. for (i = 1; i <= 10; i++)
  11. {
  12. Console.Write(i + "\n");
  13. }
  14. Console.ReadKey();
  15. }
  16. }
  17. }

Output :

C Sharp Language Basic for-loop 1 to 10 number show example

Nested for-loop :

A loop may contain another loop in his body This form of a loop is called nested loop. But in a nested loop, the inner loop must terminate before the outer loop.

Syntax :

for (initialization; condition; update)
{
for (initialization; condition; update)
{
loop's body;
}
}


Example :

  1. /* Description: Triangle with number print */
  2. using System;
  3. namespace DemoProgramming
  4. {
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. int i,j;
  10. for (i = 1; i <= 20; i++)
  11. {
  12. Console.Write("\n");
  13. for (j = 1; j <= i; j++)
  14. {
  15. Console.Write(j + " ");
  16. }
  17. }
  18. Console.ReadKey();
  19. }
  20. }
  21. }

Output :

Triangle with number print

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