In this post we will discuss about Loops in C#.net.
Also you can check out:
- How to give Tooltips in WPF in Asp.Net?
- How to validate user input for mobile number using Regular Expression in Javascript?
- Get last modified time of a file in C#.Net
If you are preparing for interviews, then this interview questions on Asp.Net, ADO.Net, WCF and Ajax will help you.
- Loops helps to repeat a segment of code multiple times.
- C#.Net provides 3 types of Loops: for loop, foreach loop and while or do...while loop.
for Loop:
- Through for loop you to repeat a block of code a set number of times, using a built-in counter. To create a for loop, you need to specify a starting value, an ending value, and the amount to increment with each pass.
syntax:
for (int i = 0; i < 10; i++)
{
// Code which will execute 10 times.
}
foreach Loop:
- foreach loop allows you to loop through the items in a set of data. The code will then loop until you’ve had a chance to process each piece of data in the set.
Example:
int[] intArray = {1,2,3};
foreach (int num in intArray)
{
num += 1;
}
while loop:
- A while loop tests a specific condition before or after each pass through the loop. When this condition evaluates to false, the loop is exited.
int i = 0;
while (i < 10)
{
i += 1;
}
Here the loop will execute till the value of i >=10. Here it will loop 10 times.
- The do..while syntax is also same. In case of do...while the condition will be tested at the end of each pass through the loop.
syntax:
int i = 0;
do
{
i += 1;
}
while (i < 10);
- The do...while loop will always execute the code at least once, because it doesn’t test the condition until the end.
Also you can check out:
- How to give Tooltips in WPF in Asp.Net?
- How to validate user input for mobile number using Regular Expression in Javascript?
- Get last modified time of a file in C#.Net
If you are preparing for interviews, then this interview questions on Asp.Net, ADO.Net, WCF and Ajax will help you.
- Loops helps to repeat a segment of code multiple times.
- C#.Net provides 3 types of Loops: for loop, foreach loop and while or do...while loop.
for Loop:
- Through for loop you to repeat a block of code a set number of times, using a built-in counter. To create a for loop, you need to specify a starting value, an ending value, and the amount to increment with each pass.
syntax:
for (int i = 0; i < 10; i++)
{
// Code which will execute 10 times.
}
foreach Loop:
- foreach loop allows you to loop through the items in a set of data. The code will then loop until you’ve had a chance to process each piece of data in the set.
Example:
int[] intArray = {1,2,3};
foreach (int num in intArray)
{
num += 1;
}
while loop:
- A while loop tests a specific condition before or after each pass through the loop. When this condition evaluates to false, the loop is exited.
int i = 0;
while (i < 10)
{
i += 1;
}
Here the loop will execute till the value of i >=10. Here it will loop 10 times.
- The do..while syntax is also same. In case of do...while the condition will be tested at the end of each pass through the loop.
syntax:
int i = 0;
do
{
i += 1;
}
while (i < 10);
- The do...while loop will always execute the code at least once, because it doesn’t test the condition until the end.