ASP.NET 2.0 All-In-One Desk Reference For Dummies®
by Doug Lowe, Jeff Cogswell, Ken Cox - Microsoft MVP
Chapter 3. Going Around in Circles (Or, Looping in C#)
In This Chapter
✓ | The thrill of while loops |
✓ | The brilliance of break statements |
✓ | The rapture of infinite loops |
✓ | The ecstasy of the continue statement |
✓ | The splendor of do loops |
✓ | The wonder of for loops |
✓ | The joy of nested loops |
So far, all the programs in this book have started, run quickly through their main method, and then ended. If Dorothy from The Wizard of Oz were using these programs, she’d probably say, “My, programs come and go quickly around here!”
In this chapter, you find out how to write programs that don’t come and go so quickly. They hang around by using loops, logical constructions that let them execute the same statements more than once.
Or, put another way, loops are like the instructions on your shampoo: Lather. Rinse. Repeat.

Like if statements, loops rely on conditional expressions to tell them when to stop looping. Without conditional expressions, loops would go on forever, and your users would grow old watching them run. So, if you haven’t yet read Chapter 2 of this mini-book, I suggest you do so before continuing much farther.
Your Basic while Loop
The most basic of all looping statements in C# is while. The while statement creates a type of loop (called, you guessed it, a while loop) that executes continuously as long as some conditional expression evaluates to true. Since while loops are useful in all sorts of programming ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access