While and Do-While Loops
While Loops
Exit when condition becomes false
int i = 0;
// Loops 'while' i is less than 10. Exists when i is 10 or more.
while (i < 10)
{
Console.WriteLine(i);
i++; // Without incrementing i, the loop will become an infinite loop
}Exit when a condition is met inside the loop
int i = 0;
// Infinite loop by design. Must have a way to exit in the code within the loop
while (true)
{
Console.WriteLine(i);
i++;
if (i >= 10)
{
break;
}
}Do-While Loops
Last updated