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
  • PrintPrint
Share this Page URL
Help

7. Loops > The do-while loop

The do-while loop

None of the cool kids use the do-while loop, but for completeness, here it is. The do-while loop doesn’t check the expression until it has executed the block. Thus, it ensures that the block is always executed at least once. If you rewrote the original exercise to use a do-while loop, it would look like this:

int main(int argc, const char * argv[])
{
    int i = 0;
    do {
        printf("%d. Aaron is Cool\n", i);
        i++;
    } while (i < 13);
    return 0;
}

Notice the trailing semicolon. That’s because unlike the other loops, a do-while loop is actually one long statement:

do { something } while ( something else stays true );

Here’s a flow-chart of this do-while loop:

Figure 7.5  do-while loop


  

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