Loops

Loop constructs are all about executing code iteratively, and as you'd expect, C# supports the standard loops—for, while, and do...while. It also includes another loop construct you might not expect—the foreach loop, which lets you loop over C# collections (coming up in Chapter 6) and arrays. We'll see all these loops in this chapter.

The for Loop

The for loop is the most basic loop, and it's a programmer favorite. It simply executes a statement (or a block of statements) repeatedly until a test expression evaluates to false. Here's what it looks like:

for ([initializers]; [expression]; [iterators]) statement
					

Here are the parts of this statement:

  • initializers A comma-separated list of expressions (possibly including assignment statements) ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.