![]() |
Do...Loop cycle.
As we saw on the previous chapter, the For...Next cycle accumulates a value in an accumulator to count up the rounds that the cycles has given, and stop when the end of the cycle is reached.
With the Do...Loop cycle, the limit will be given by the result of a logic evaluation. The cycle will continue going around until the logic evaluation returns the expected value. As an example:
Do While return_do = "" return_do = InputBox("Ingrese un valor para el ciclo Do:","Ciclo Do") Loop |
For each round of the cycle, an InputBox will pop up asking the user to input some data (a character, a number, etc). If the user doesn´t input a value (clicks Cancel) the InputBox returns a string of length 0 (zero), which means 'nothing' and the evaluation in the cycle doesn´t fullfill, so the cycle takes one more round (?).
Let´s take a closer look at the cycle´s definition:
Do While return_do = ""
If we think of this as pure english that´s a very straigh-forward directive (don´t you think?)
Logically speaking, it means that as soon as return_do contains something else than nothing the Do...Loop cycle must end. Again: the cycle will continue the rounds provided return_do is empty. If the user puts some value in return_do (that is to say, types on the InputBox) the Do...Loop cycle ends.
But doing it is always better than talking about it. And so, I suggest you to try it. Put that code on a code sheet inside the code definition of a button, run the proggy, and click on the button. You´ll see that as long as you don´t put anything on the InputBox, the program shows the InputBox again, wich means, it´s not coming out of the cycle. But if you do type something, it can´t keep going because the condition return_do = "" is not fullfilled and it must continue with whatever is following the cycle´s block on the whole code.
I´ll show you now, a small proggy that uses the Do...Loop cycle to save some sequential data in a "register" file to record the prescence of employees at an office:
Private Sub Command1_Click Open "dayly_registration.Txt" For Append As #1 Do While return_do <> "finish" return_do = InputBox("Type-in your name:", "Dayly Registry") Print #1, return_do & " checked in at:" & Time$ Loop Close #1 End Sub |
When the sys-op checked the last employee in, types "finish" at the InputBox and the registry is closed until the program is re-executed (the next day).
You should remember, that you can give any condition on the logic evaluation, that would have been valid at an if block. As an example, you can have control over the Do...Loop cycle, with the following definitions:
Do While return_do = "finish"
Do While return_do = 0
Do While return_do < 100
Do While Text1.Text <> ""
Very well, I hope this tutorial tought you more than the one before.
I invite you now to pass to the next chapter, where we´ll be treating the subject of the While...Wend cycle: