Intermediate Programming Instructor: Greg Shaw

Intermediate Programming Instructor: Greg Shaw

<p>Computer Programming I Instructor: Greg Shaw COP 2210 </p><p>Loop Pitfalls and the repeat-until Construct</p><p>I. Make sure every loop can be entered and exited</p><p>Beware of “impossible” conditions (can never be true) and “unavoidable” conditions (are always true)</p><p>II. Beware of “off by one” errors</p><p> Definition: A loop does one too many or one too few iterations</p><p> This is a very common logic error when using loops</p><p> Probable causes: o Erroneous initiation of the loop control variable. E.g., initializing to 0 instead of 1, or vice-versa o Error in the boolean expression. E.g., using “<” instead of “<=” (or vice-versa). Or “>=” instead of “>” (or vice-versa)</p><p>III. The repeat-until construct</p><p> In all three Java loops – while, for, and do-while – the boolean expression is the loop continuation condition. I.e., as long as the boolean expression is true, another iteration will be done</p><p> Sometimes, however, it may be easier to think of the condition for exiting a loop rather than for continuing it. Algorithmically, this is known as the repeat-until construct, and looks like this: repeat { statements } until (boolean expression)</p><p> The statements in the loop body will be executed until the boolean expression becomes true. In other words, the boolean expression is the loop exit condition  Like the Java do-while, this is a post-test loop so the loop body will always be done at least one time</p><p> Some languages have a repeat-until loop. Although Java does not, it is easy to implement one using the do-while loop</p><p>IV. Implementing the repeat-until in Java</p><p>Since the boolean expression in the repeat-until is the condition for exiting the loop, the loop continues as long as the condition is false, or as long as the opposite condition – the negation - is true.</p><p>This is easily implemented using the Java do-while. The do- while on the right is the Java equivalent of the repeat-until construct on the left:</p><p> repeat do { { statements statements } } until (boolean-expression) while (!boolean-expression) ;</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us