// # An exit condition that’s never reached
byte i = 0;
while (true)
{
// ...
Console.WriteLine($"i = {i}");
i++;
if (i > 275)
{
break;
}
}
// Fixed: loop variable now an integer
int i = 0;
while (true)
{
// ...
Console.WriteLine($"i = {i}");
i++;
if (i > 275)
{
break;
}
}
// # A condition that makes the loop start over and over again
int i = 0;
// Loop 1,000 times at most
while ((i = 1) < 1000)
{
Console.WriteLine($"i = {i}.");
// ...
if (i > 125)
{
break;
}
}
int i = 0;
// Loop 1,000 times at most
while ((i += 1) < 1000)
{
Console.WriteLine($"i = {i}.");
// ...
if (i > 125)
{
break;
}
}
// # Change the loop variable to a new value inside the loop
int i = 0;
while (i < 10)
{
// ...
i = 3;
// ...
Console.WriteLine($"i = {i}");
i++;
}
int i = 0;
while (i < 10)
{
// ...
Console.WriteLine($"i = {i}");
i++;
}
// # A loop without an exit condition
int i = 0;
while (true)
{
Console.WriteLine($"i = {i}");
i++;
}
int i = 0;
// Fixed: add an exit condition to the loop
while (i < 10)
{
Console.WriteLine($"i = {i}");
i++;
}
int i = 0;
while (true)
{
Console.WriteLine($"i = {i}");
i++;
// Fixed: terminate the loop with 'break'
if (i > 9)
{
break;
}
}