While Loop in CSharp With using example

Introduction

In this article step by step we will understand about while loops in c-sharp with an example and when do you use break and continue statements inside while loop using c# code examples.

Why Loops

If we want to perform the repetitive tasks "n" number of times or infinite number of times then it is good practice to use loops.

While Loop

While statement executes a statement until a specified expression evaluates to false.

If the expression evaluates to true, the while statement continues to execute the statement in the while block.

Syntax

 
while (Boolean-expression)
{
         ….Statement
}    

Example

 
int num = 10;
while (num > 0) {
  Console.WriteLine("Current value of num is {0}", num);
  num--;
}    

Output of above program will be

So hey friends this about the while loop with an simple code example. Now let's see usage of break and continue statements inside the while loops.

Break Statement in while loop C#

Break forces a loop to exit immediately.

If we want to terminate while loop in between of iteration or before condition reaches to false then use break statement.

In order to use break statement in while loop we have to use "break" keyword.

Example of Break Statement

 
int num = 10;
while (num > 0) {
  Console.WriteLine("Current value of num is {0}", num);
   if (num == 4){
          Console.WriteLine("Breaking at {0}", num);
          break;
    }
  num--;
}    

Above code loops the code in descending order and prints each descending number finally breaking at 4 and loops terminates at 4.

So as you see from our above example "break" keyword forces a loop to exit immediately.

Output

Continue Statement in while loop C#

A continue statement within body of while loop will stops the execution of the code and continues to re start it again.

If we want to re-evaluate the code in a loop we use continue statement in a loop.

In order to use continue statement in while loop we have to use "continue" keyword.

Example of continue statement

 
int num = 10;
while (num > 0) {
  Console.WriteLine("Current value of num is {0}", num);
   if (num == 4){
          Console.WriteLine("Breaking at {0}", num);
          continue;
    }
  num--;
}    

Above code loops the code in descending order and prints each descending number finally again re starting when the loop reaches to 4.

So as you see from our above example "continue" keyword forces a loop to exit immediately.

Friends this is all about while loop using break and continue statements inside while loop. If you have any doubts please share your doubts in the comment section below and if you like this article kindly share it with your friends.

Author: Gurunatha Dogi

Gurunatha Dogi

Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more

Comments

64x64
By Muhammad Ali on 2014-09-19
Congrat ,you done a superb job .I really like your approach.which is focus and result oriented.You have a great ability to convert difficult task into simpler one.I pray for your career and your life.
64x64
By MohitKumar on 2014-04-15
Nice explanation i really understood for learning more about c# interview question you can prefer this link also c# interview questions

Add a Comment