Updated March 16, 2023
Introduction to C# While Loop
C# while loop can always run until the break keyword, is utilized or it can condition can be false. The while declaration executes an argument or a block of statements while a particular Boolean expression examines to true. Since that expression is examined prior to every execution with the loop, a while loop executes zero or even more occasions. This kind of differs through the do loop, which usually executes more than one occasion. At any time inside the while declaration block, you may break out of that loop utilizing the break declaration. It is easy to step straight to the analysis with the while expression utilizing the continue declaration. In case the expression examines true, execution proceeds with the first declaration in the loop. Or else, performance proceeds with the first declaration following the loop.
Syntax for C# While Loop
Given below is the syntax mentioned:
While (Boolean expression like true or false)
{
//execute program so long as state returns true
}
Example:
Code:
The while loop which is equally pretty straightforward. A while loop, just imagines what it is like an if statement except when you reach the bottom brace, you go back to the top. So we are going to Int x = 10, while x is less than 15, Console.WriteLine(x), and then we will increment x. So that’s just adding one to the value of x. So this will enter here, check initially, check the condition, and 10 < 15 so we say yes, write it out, and enter the body of the while loop above increment x to 11, and when we hit while loop brace { } we go up to the top again. That will evaluate x less than 15 (x < 15 ), and on we go until we have evaluated, until we have incremented to x to the point where it is 15, at which point, when we get to the bottom here, go back up, reevaluate, we will say okay, it is not anymore. So we will come out. And then we put a “Final value of x” and write that. So I would expect this to print 10, 11, 12, 13, 14, Final value of x: 15.
Output:
Example of the Break Keyword
Code:
static void Main (string[] args)
{
….
var repeat = true; // A new variable repeat to be used within our while loop’s condition
while(repeat)
{
//This will run forever until loop is false or the break keyword is used
}
}
Note: Be careful applying loops, the above example has no way to exit the loop creating
what’s referred to as an infinite loop
The break keyword goes out the loop at the stage it’s called.
…
while(repeat)
{
Console.WriteLine(“Add”, Announce, or Quit”);
…
else if(action == “Quit”)
{
break; // The loop can exit at this time skipping any kind of remaining code in the loop
}
…
}
…
So there are two things that you often want to do almost always conditionally. They are basically a way of either coming out of a loop early. So even if we know that x < 15 conditions are still going to hold, we want to come out because our time’s up or whatever it is. So if x % 3 == 0, break . And this will break out of the nearest enclosing loop. Which can be a while loop and any other kind of loops that we have forgotten. So we put if condition after the Console.WriteLine(x), so what we are going to do int x = 10, check that number, we don’t need to break, So x++, increment to 11, print 11, next increment to 12, write out 12, decide to break at this point and this does not go for the increment. So we are going to end up printing out “Final value of x is 12”. So when we run this we should see 10, 11, 12, final value 12.
Output:
Now let’s try doing this slightly differently. Now, we could end up in problems if we just did this as continue.
Code:
At this moment this would be an infinite loop. But let’s just change something to say x += 2;
Code:
So this is now a ContinueInWhile(). This time we will start off with 10. Print it out, see whether x % 3, if it is we are going to add 2 to it means x + = 2, and then continue, which means skipping this x++.
Code:
So let’s even write this out. Console.WriteLine(“Skipping”). So this time we would expect to see it will print 10, then test this and 10 is not a multiple of three means if(x % 3 == 0) condition. So we go on to 11, print 11, 11 is not a multiple of 3, go on to 12, print out 12, 12 is a multiple of 3. So we are going to print “Skipping”, then increment x += 2, which means x to 14. Continue, so go to this closing brace which then goes up, check for the x < 15, yes it is, prints out 14, not a multiple of 3, increment it to 15 and now when we check the condition it’s false. So we will print the final value of x is 15. So we should see 10, 11, 12, Skipping, 14, Final value of x.
Output:
Flowchart of C# While Loop
- In the while loop, the condition could show up prior to the body of the loop.
- Should the condition can be FALSE at first, while loop will never be executed.
- While it can be an entry-controlled loop.
- While initially view the condition, after that enter the body.
Conclusion
C# features a rich group of statements used to manage the flow of execution within your code. Although in while loop just initialization as well as, condition reaches the top of the body of the loop as well as, iteration might be created anywhere in the body of that loop. Iteration statements (for, for each, while, and do) Utilized to put in place loops. All these loops offer different features which make it pretty much useful in particular circumstances. The for loop is quite beneficial once statements should be executed a particular quantity of occasions; the for-each loop is utilized to iterate more than every item within a collection. The while loop executes statements provided a managing expression examines as true; the do loop is just like the while loop but ensures the fact that managed statements execute at least one time.
Recommended Articles
This has been a guide to C# While Loop. Here we discuss the introduction, flowchart of the while loop along with syntax and example. You can also go through our other suggested articles to learn more –