Chapter 7

Statement Repetition Using Loops

What you will learn in this chapter:

  • 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

wrox.com code downloads for this chapter

You can find the wrox.com code downloads for this chapter at www.wrox.com/remtitle.cgi?isbn=9781118336922 on the Download Code tab. The code in the Chapter07 folder is individually named according to the names throughout the chapter.

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

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

Program Loops

If you're sitting in a public building right now, look at the ceiling; chances are you can see sprinklers embedded there. In most modern office buildings, each room has one or more sprinklers in it. Modern sprinklers are monitored by software that tests each one to see if it senses a fire. If a building has 500 sprinklers, the program samples the current state of sprinkler number 1 and, assuming everything's okay, goes on to test sprinkler number 2. This process continues ...

Get Beginning Object-Oriented Programming with C# 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.