Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Iteration

Conditional statements play a very important role in programming. They are the workhorse of every program. But they cannot do the job alone. In every program, we deal with the situation when we have to repeat the same sequence of statements, for different customers, transactions, online clients, and so on. These tasks require iteration.

For repeated actions, C++ provides three iterative statements: while loops, do-while loops, and for loops. Each kind of C++ loop controls the repetition of a single statement (which ends with a semicolon) or a compound statement (block) enclosed in braces (and there is no semicolon after the closing brace of the block). To control iterations, all types of loops use logical expressions similar to logical expressions used in conditional statements. These logical conditions evaluate to true or false (nonzero or 0). They are tested for each loop repetition. When the loop condition becomes false, the iterations are terminated. If the condition is true, the body of the loop (the statement or the block) is repeated. Notice that I said "for each loop repetition" and not "before each loop repetition" because the loops differ in the way in which the loop conditions are tested. No matter what the loop design, the loop body has to do something that affects the loop condition. If that does not happen, the loop condition might stay true forever—a serious menace for every program that uses loops.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial