OneCompiler

Loops

1. For

For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations are known in advance.

Syntax

for(Initialization; Condition; Increment/decrement){  
//code  
} 

Example

// print 1 to 10 numbers in C# using for loop
using System;

namespace Loops
{
	public class Loops
	{
		public static void Main(string[] args)
		{
	
		  for (int i = 1; i <= 10; i++) {
          Console.WriteLine(i);
      }
			
		}
	}
}

Check Result here

2. While

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations is not known in advance.

Syntax

while(condition){  
//code 
}  

Example

// print 1 to 10 numbers in C# using while loop
using System;

namespace Loops
{
	public class Loops
	{
		public static void Main(string[] args)
		{
        int i = 1;
		    while (i <= 10) {
        Console.WriteLine(i);
        i++;
        }
	  }
	}
}

Check result here

3. Do-while

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

Syntax

do{  
//code 
} while(condition); 

Example

// print 1 to 10 numbers in C# using do-while loop
using System;

namespace Loops
{
	public class Loops
	{
		public static void Main(string[] args)
		{
      int i = 1;
      do {
      Console.WriteLine(i);
      i++;
      } while(i <= 10 );
			
		}
	}
}

Check result here