while loop C# code example

Example 1: c sharp exit while loop

// To forcibly exit a while loop use 'break'
while (true)
{
	// Do something
  
	if (conditional)
    {
    	break;
  	}
}

Example 2: while loop in c#

int i = 0;

while (i < 10)
{
    Console.WriteLine("Value of i: {0}", i);

    i++;
}

Example 3: c# while loop

while(condition) 
{
   statement(s);
}
_______________EX.___________________
int a = 0;
while (a < 10)
{
	Console.WriteLine(a); //output>> 0 .. 1 .. 2 ........ 8 .. 9
	a++;
}

Example 4: c# do while loop

do {
   statement(s);
} while( condition );
_______________EX.___________________
int i = 0;

do
{
	Console.WriteLine(i);
	i++;
}
while (i < 5);

============OUTPUT======================
  0
  1
  2
  3
  4

Example 5: c# while loop

int n = 0;
while (n < 5)
{
    Console.WriteLine(n);
    n++;
}

Example 6: while c#

int repeats = 0; 
bool while = true; //creates a bool variable with the true value
while (while = true) //repeats the code in the curly brackets as long
{                    //as the value in the round brackets remains true
   repeats++; //increase the value inside repeats
}