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: Difference between revisions

From Groundhog Learning
Updated via bot
 
(No difference)

Latest revision as of 16:05, 21 October 2025

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