JMA201-01
Fundamentals of Application Development

DEPARTMENT SCHEDULE SYLLABUS RESOURCES
Loops Continued - Do-While

Continuing our discussion of loops, consider how we described the for loop - it is a structure that will iterate a specific number of times. Either the programmer knows at design-time, or the user will enter a value at run-time, that will determine exactly how many times to iterate. However, sometimes we just don't know how often a loop should run - it should run "until it's done".

In this case, we need to work with a different type of loop - the do/while or do/until. Quite simply, these are variations we call the do loop; it runs until the expression it contains evaluates to either true or false. Do while will run as long as the specified expression is true; do until will run until the specified expression evaluates to true (meaning, run as long as it is false).

Both of these structures can appear in either pretest or posttest form. Pretest form means to check the expression before running the loop; posttest means to check the expression after the first lap, to decide if any more laps should be run. If you need to be sure the loop iterates at least once, use the posttest form. Before we show you sample loops, we need to cover one more thing. Do loops do NOT have an integrated counter variable. Therefore, you need to make sure one of the variables involved in the expression being changed is modified somehow inside the body of the loop; otherwise, the loop would never end, and we'd have an infinite loop that would crash our program! With that said, here is a sample do loop based on the example files:

  Do
      monthlyInterest = balance * interestRate
      balance = balance + monthlyInterest
      balance = Math.Ceiling(balance - payment)
      numberOfPayments = numberOfPayments + 1
  Loop Until balance <= 0

In this form (posttest) the loop is guaranteed to run at least once (because the test is at the bottom!) The loop is going to run until the balance variable is less than or equal to zero; note that I make sure to change balance during every lap of the loop.

I could write practically the same loop with while instead of until, simply by changing the expression:

  Do
      monthlyInterest = balance * interestRate
      balance = balance + monthlyInterest
      balance = Math.Ceiling(balance - payment)
      numberOfPayments = numberOfPayments + 1
  Loop while balance > 0

In this form (posttest) the loop is guaranteed to run at least once (because the test is at the bottom!) The loop is going to run while the balance variable is greater than zero. The two types of loop are roughly equivalent - only the expression needs to be changed. Until will run as long as the expression balance<-0 is false; while will run as long as the expression balance>0 is true.

The other form of these statements is the pretest. In this form, we move the expression being tested to the top; note, if the expression evaluates to false the first time, the loop won't run at all! Here are the examples:

  Do while balance > 0
      monthlyInterest = balance * interestRate
      balance = balance + monthlyInterest
      balance = Math.Ceiling(balance - payment)
      numberOfPayments = numberOfPayments + 1
  Loop 

Or...

  Do until balance <= 0
      monthlyInterest = balance * interestRate
      balance = balance + monthlyInterest
      balance = Math.Ceiling(balance - payment)
      numberOfPayments = numberOfPayments + 1
  Loop