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 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.