Stop a while loop before it naturally finishes using a break statement.
Break a While Loop in C#
💻 C#
bool playerIsAlive = true;
while (playerIsAlive)
{
if(playerQuitsGame)
{
break; // Break early from the loop.
}
// Run game logic.
}
// Exit the game.
Related Concepts
- Uses: Break Statement
- Uses: While Statement