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

Write a While Statement

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)

Use a while loop to repeat a block of code as long as a condition is true.

Write a While Statement in C#

💻 C# Basic Syntax

while (<CONDITION>)
{
    // statements to repeat
}

💻 C# Example

int energy = 5;

while (energy > 0)
{
    Console.WriteLine("Player runs");
    energy--; // decrease energy to eventually end the loop
}

Related Concepts