Which loop structure is best

As a general rule, it is best to use the loop that makes for the cleanest and clearest comparison test. Sometimes, the logic makes the Do While clearer, whereas other loops seem to work better when you set them up with Do Until. Remember, Do Until continues executing a block of VBA statements as long as the comparison test is false. As soon as the test becomes true, the loop terminates and the program continues on the line that follows the closing Loop statement. Remember also, that when the comparison test appears at the bottom of the loop instead of at the top of the loop, the body of the loop will be guaranteed to execute at least one time. Whereas, when the comparison appears at the top of the loop, then depending on the test comparison the loop may not execute even once.

Listing 7.7 is a version of the age-checking program that's much shorter than the previous ones. The comparison test appears at the bottom of the loop, so the extra InputBox function call is not needed.

0 0


  • melampus
    Which loop structure is best?
    8 years ago

Post a comment