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.

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Mar
20
2014

How to deploy an assembly into GAC(Global Assembly Cache) ?

CLICK TO READ FULL ARTICLE

Views - 15783 |Category - C#

Mar
14
2014

Understanding concept of Concat,Substring methods of a string class ?

CLICK TO READ FULL ARTICLE

Views - 4699 |Category - C#

Mar
10
2014

C# if and nested if conditional statements with an example?

CLICK TO READ FULL ARTICLE

Views - 11772 |Category - C#

Mar
08
2014

Practical - Count repeated words in a string using Regular Expression ?

CLICK TO READ FULL ARTICLE

Views - 6133 |Category - C#

Dec
07
2013

What are generic collections in C# using an example?

CLICK TO READ FULL ARTICLE

Views - 34705 |Category - C#

 
Author: 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, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

esm9d

 

 
Comments

Posted 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.

Posted 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