O'Reilly logo

Microsoft®C# Programming for the absolute beginner by Andy Harris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Varying the For Loop’s Behavior

Several variations of this basic loop are used for different kinds of counting situations. You can set up for loops that skip over numbers, count by two, or count backwards. The basic design of the for statement is the same, but you can change the way the three parts of the for loop are written to change the way the loop acts.

The Fancy Beans Program

The Fancy Beans program demonstrates a few variations on the theme of the basic for loop. Take a look at the output of this program in Figure 3.6, then I’ll show you how the code works.

Figure 3.6. This bean counter can count by fives, backwards, and pulls the words out of a sentence one at a time.

Take a look at the code for the fancier bean counter, and I’ll explain the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required