Ex11ControlSummary

Ex11ControlSummary - Three loops while do-while for How to...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Three loops while do-while for How to choose? while . You can implement any loop as a while loop. This is used especially for sentinel-controlled loops that many execute any number of times. do-while . This may be used for any loop that you know will execute at least once, but it is appropriate only for sentinel- controlled loops. If you use a do-while loop for a loop that could execute (depending on user input) 0 times, then you will get incorrect results. for . This may be used for any loop, but it is typically appropriate for counter-controlled loops. Let us take an example of a counter-controlled loop that will execute 10 times. In this case, you can use any of the three loops, though a for loop is the most appropriate. while loop: int counter; counter = 0; while(counter < 10) { // do some work counter++; } do-while loop: int counter; counter = 0; do { // do some work counter++; } while(counter < 10); for loop: int counter; for(counter = 0; (counter < 10); counter++) { // do some work
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/27/2011 for the course CMPSC 5 taught by Professor Franklin during the Fall '09 term at UCSB.

Page1 / 3

Ex11ControlSummary - Three loops while do-while for How to...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online