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