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
Revision as of 16:05, 21 October 2025 by Groundhog (talk | contribs) (Updated via bot)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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