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 C# to begin a loop by starting a counter at a specific value. C# 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 For

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

Table 15.1. Components of the for Statement
Part Description ...

Get Sams Teach Yourself C#™ in 24 Hours now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.