Export (0) Print
Expand All
10 out of 22 rated this helpful - Rate this topic

Closer Look: Using Do...While and Do...Until to Repeat Until a Condition Is Met

In this lesson, you will learn how to use the Do...While and Do...Until statements to repeat code based on certain conditions.

In the previous lesson, you learned how to use the For...Next statement to loop through a block of code a specified number of times—but what if the number of times that the code has to be repeated is different for certain conditions? The Do...While and Do...Until statements enable you to repeat a block of code while a certain condition is True, or until a certain condition is True.

For example, suppose you had a program to add a series of numbers, but you never wanted the sum of the numbers to be more than 100. You could use the Do...While statement to perform the addition as follows:

Dim sum As Integer = 0
Do While sum < 100
  sum = sum + 10
Loop

In this code, the Do While line evaluates the variable sum to see whether it is less than 100: If it is, the next line of code is run; if not, it moves to the next line of code following Loop. The Loop keyword tells the code to go back to the DoWhile line and evaluate the new value of sum.

To use a Do...While statement

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type DoWhile and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag one TextBox control and one Button control onto the form.

  5. Double-click the Button to open the Code Editor.

  6. In the Button1_Click event handler, type the following code:

    Dim sum As Integer = 0
    Dim counter As Integer = 0
    Do While sum < 100
      sum = sum + CInt(Textbox1.Text)
      counter = counter + 1
    Loop
    MsgBox("The loop has run " & CStr(counter) & " times!")
    
  7. Press F5to run the program.

  8. In the text box, type a number and click the button.

    A message box appears that displays how many times the number was added to itself before reaching 100.

  9. On the Debug menu, click Stop Debugging to end the program. Keep this project open. We are about to add to it.

The Do...While statement repeats a loop while a condition remains True, but sometimes you might want your code to repeat itself until a condition becomes True. You can use the Do...Until statement as follows:

Dim sum As Integer = 0
Do Until sum >= 100
  sum = sum + 10
Loop

This code resembles the code for the Do...While statement, except that this time the code is evaluating the sum variable to see whether it is equal to or greater than 100.

This procedure starts where "To use a Do...While statement" ended. If you have not completed "To use a Do...While statement," you must do so before continuing.

To use a Do...Until statement

  1. Add the following code underneath the MsgBox line.

    Dim sum2 As Integer = 0
    Dim counter2 As Integer = 0
    Do Until sum2 >= 100
      sum2 = sum2 + CInt(Textbox1.Text)
      counter2 = counter2 + 1
    Loop
    MsgBox("The loop has run " & CStr(counter2) & " times!")
    
  2. Press F5 to run the program.

  3. In the text box, type a number and click the button.

    A second message box appears that displays how many times the number was added to itself before equaling 100 or greater.

In this topic, you learned how to use the Do...While and Do...Until loops to repeat code conditionally. At this point, you can go on to the next lesson, Making a Program Choose Between Two Possibilities: The If...Then Statement.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.