C# tutorial Loops


C# loops

Put simply, loops enable your program to execute the block of code repeatedly. In C# there are four loops: for, while, do while, and foreach loops.

-For loop

for(start value;condition;increment){


statement1
statement2

}

The code in for loop will be executed from the start value until the condition is met. The increment will be added continuously to the start value until the condition is met.

Example:
   
       int   i;
      for (i = 1; i <=10; i++)
         Console.WriteLine("i={0}",i);/*The value of i will be printed 10 times */

-While Loop

The while loop executes code repeatedly until a condition is met. Without a false condition, the statements in while loop are executed infinitely. Therefore, make use you provide a stop point (false condition) where it should stop.

 

while(condition){

statement

statement

---------

}

 

Example:

int i = 1;
while (i <= 10)
{
   Console.WriteLine("i2={0}",i*i);
   i = i + 1;
}

-Do While Loop

The do while loop tests the condition at the bottom of the loop. It executes at least one time although the condition is not true.

Example:

int i = 1;
do
{
   Console.WriteLine("i3{0}=",i*i*i);
   i = i + 1;
}
while (i <= 10);

-Foreach loop

The foreach loop is used to iterate through a collection object, namely array.

Example:

   string value;

   string key;

   foreach (DictionaryEntry ev in Environment.GetEnvironmentVariables())

                {

                    key = (string)ev.Key;

                    value = (string)ev.Value;

                    if (key.StartsWith("P"))

                        Console.WriteLine("{0},{1}", key, value);

                }

                Console.WriteLine();






Comments

MohitKumar comment

 MohitKumar

Check this for c# interview questions http://skillgun.com/csharp/interview-questions-and-answers


2014-08-12
Dara comment

 Dara

You need to convert the number to an array of characters then use Linq to group digits and count the number of digits in every group.
string strnum = "1234431134456765";
char[] chs = strnum.ToCharArray();
var gs = from ch in chs
group ch by ch into newgroups
select newgroups;
foreach (var g in gs)
{
Console.WriteLine("{0}={1} times",g.Key,g.Count());
}
Console.Read();


2014-01-28
Athomy Shaka comment

 Athomy Shaka

Hi every body! I have a question and need your help. How can i count duplicate digits in a number? For example, if i have 1234431134456765, the output of the program will be:
1=3 times
2=1 times
3=3 times
4=4 times
5=2 times
6=2 times
7=1 times


2014-01-28
CAPTCHA image




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.