Select Page

While Loop C#

The while loop C# is a statement that continues to repeat on a loop until the condition of its expression is no longer true. If you know with any other OOP languages, you’ll probably be familiar with a similar method, as the while loop exists in some form in any OOP language.

In Python, the syntax for the while loop looks something like this:


The above example will function as a while loop if the condition is an expression that is true. If the condition is not true, the loop won’t be executed. If it is true, the loop will be executed until the condition becomes false, but either way, the conditional expression should be one that results in a boolean value (true or false) in order for this loop to work correctly. The statement of the loop can be any function or code you’d like to be executed while the condition is true. Check out the example below to see how the while loop works in context:

i = 0

The output of the example above will be:


In the while loop above, the conditional expression tells the code to continue as long as the variable i is less than 4, and each time the code executes, i increases by 1 (i++), so the code will execute when i is 0, when i is 1, when i is 2, and when i is 3, but when i becomes 4, the conditional expression is no longer true, so the loop ends.

Share This