While Loop, Do-While Loop In C#
In our previous post we learned about for-loop and understood a bit about how to call a statement again and again. In this post we will learn about while loops. Let's understand what these while loops are and how they function.
while loop consists of just a single component:
This is a basic example of while loop. Here i is the loop control variable whose value is incremented by 1 after each iteration. And every-time before Console.WriteLine(i); statement is executed, condition part of the loop is validated and as we can see, when the value of i reaches 5, the loop exits and hence the last printed value of i is 4.
This is an example of do-while loop. First part shows the basic syntax and output of the loop, just like while loop but with an extra time statement is executed. In second part of example, notice the condition will return false but still Console.WriteLine(i); is executed and printed 0 in output. This is happening because do-while executes the statement before checking the condition part.
Try yourself and find out various usages of these loops. Let us know if you any query or concerns. Happy Reading (refer full series here - C# Tutorial Index)
While Loops Are Of Two Different Types, While & Do-While Loop
Basic Structure Of 'while' Loop
while loop consists of just a single component:
- Condition
Condition: If the condition returns true then the loop continues to execute otherwise the loop exits out.
Shown below is the basic structure of while loop
Shown below is the basic structure of while loop
while(condition) { //Code which is to be executed again and again }
Example Of while Loop
int i = 0; while(i < 5) { Console.WriteLine(i); i++; } //Output //0 //1 //2 //3 //4
This is a basic example of while loop. Here i is the loop control variable whose value is incremented by 1 after each iteration. And every-time before Console.WriteLine(i); statement is executed, condition part of the loop is validated and as we can see, when the value of i reaches 5, the loop exits and hence the last printed value of i is 4.
Do While Loop
do-while loop is similar to while loop but with a slight change in functionality. do-while loop executes the statement first and then checks the condition. So in case of do-while loop, the statement will be executed at-least once even when the condition returns false, whereas in case of while loop statement will never be executed.
Example Of do while Loop
int i = 0; do { Console.WriteLine(i); i++; } while(i < 5); //Output //0 //1 //2 //3 //4
//5 //Another Example int i = 0; do { Console.WriteLine(i); } while(i < 0); //Output //0
This is an example of do-while loop. First part shows the basic syntax and output of the loop, just like while loop but with an extra time statement is executed. In second part of example, notice the condition will return false but still Console.WriteLine(i); is executed and printed 0 in output. This is happening because do-while executes the statement before checking the condition part.
Be cautious with the condition you put in there, it might go into infinite loop if the condition always returns true.
Comments
Post a Comment
Hey there, liked our post. Let us know.
Please don't put promotional links. It doesn't look nice :)