Looping a Specific Number of Times Using for Statements

The simplest type of loop to create is the for loop, which has been around since the earliest forms of the BASIC language. With a for loop, you instruct Visual C# .NET to begin a loop by starting a counter at a specific value. Visual C# .NET then executes the code within the loop, increments the counter by a defined incremental value, and repeats the loop until the counter reaches an upper limit you've set. The following is the syntax for the basic for loop:

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

Initiating the Loop Using the for Statement

The for statement both sets up and starts the loop. The for statement has the components shown in Table 14.1.

Table 14.1. Components ...

Get Sams Teach Yourself Microsoft® Visual C#™ .NET in 24 Hours 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.