|
|||
|
We've covered 2 of the 3 looping constructs available in C; the do loop and the while loop, and we've noticed the many similarities and one difference between these to methods of iteration. All of the examples we've used for these 2 looping structures have been based on incrementing a variable and testing it against a constraint to determine when to terminate the loop. It turns out that the do and while loop are well suited for iterations that don't involve an incremental variable, so that the programmer doesn't have to adminstrate the incrementation manually. The other C looping mechansim, the for loop, handles incrementation within the looping construct itself, and as a result is better suited for these types of looping scenarios. The do and while loops are better, for instance, at reading in characters from a file until an EOF (End Of File) is reached using the stdio.h function fgetc(). Reading words in from a string and comparing them to a constaining string would also be a candidate for using these two looping methods. Neither of these examples requires an incrementing variable that would need to be handled manually by the programmer. To summarize, do loops and while loops:
For another take on loops, you may wish to review the loop tutorial within the HowStuffWorks web site, which includes a potentially helpful animated graphic on how the while loop steps through it's execution. HowStuffWorks also provides an looping example that includes a good subsection on pitfalls to avoid when implementing conditional statements. In the following extend section, we'll think about a looping extension that handles incrementation within the construct, and begin considering the nesting of loops within loops in preparation for our next Module. Continue to the Extend Section -> |
||
Website by Joshua Bleier |