capypad
0 day streak
csharp / beginner
Snippet

The While Loop

A while loop repeatedly executes a block of code as long as a specified condition remains true. It is useful when the number of iterations is not known in advance.

snippet.csharp
csharp
1
2
3
4
5
int counter = 1;
while (counter <= 3) {
Console.WriteLine("Count: " + counter);
counter++;
}
Breakdown
1
while (counter <= 3)
The loop checks this condition before every execution.
2
counter++;
Increments the variable to eventually make the condition false and exit the loop.