www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons C# Loops Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Like Java and C++, C# provides a number of the common loop statements. These include while, do-while, for, and foreach.

The syntax of while, do-while, and for loops is similar to C++ and Java. Basic syntax of C# loops is given as under:

while Loop:
while (booleanExpression)
//repeating C# statement(s)


do-while Loop:
do
//repeating C# statement(s)
while (booleanExpression);

for Loop:
for (initialization; Boolean expression; update control variable)
//repearing C# statement(s)

You need to remember that unlike C++ the loop control expression must be of Boolean type.

foreach Loop:
The foreach loop was not available in C++. It is used to iterate through the values contained in any object that implements the IEnumerable interface. It has the following syntax:

foreach (variable1 in variable2)
//repeating C# statement(s)

When a 'foreach' loop runs, the given variable1 is set in turn to each value exposed by the object named by variable2. Here is an example:

int[] a = new int[]{1,2,3};
foreach (int b in a)
System.Console.WriteLine(b);

The above code outputs:

1
2
3
Next >>> Lesson No. 8: C# switch





Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.