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
To use a Do...While statement
- On the File menu, click New Project.
- In the New Project dialog box, in the Templates pane, click Windows Application.
- In the Name box, type DoWhile and then click OK.
A new Windows Forms project opens. - From the Toolbox, drag one TextBox control and one Button control onto the form.
- Double-click the Button to open the Code Editor.
- 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!")
- Press F5to run the program.
- 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. - On the Debug menu, click Stop Debugging to end the program. Keep this project open. We are about to add to it.
Do...Until Statement
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:
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.
Dim sum As Integer = 0 Do Until sum >= 100 sum = sum + 10 Loop
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
- 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!")
- Press F5 to run the program.
- 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.
No comments:
Post a Comment