Chapter 7. Statement Repetition Using Loops

Computer programs are very good at doing repetitive tasks . . . much better than humans, because computers don't get bored. In this chapter, I examine program loops. These are simply a means by which a program can repeat the execution of a given set of program statements. In this chapter you will learn the following:

  • What a loop is and when it should be used

  • What constitutes a well-behaved program loop

  • What happens when loops are ill-behaved

  • What a for loop is

  • What a while loop is

  • What a do-while loop is

  • When to use the break and continue statements

Most nontrivial programs use some form of program loop. Loops are so useful that C# makes several different types available. Let's see what loops can do for you.

Get Beginning C# 3.0 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.