Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Break a While Loop

From Groundhog Learning

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