Tambien hay una version en castellano de este post, click aca.
Welcome to this post, today we’ll talk about the «infinite» loop.
Why is it denominated as infinite? Because, it hasn’t a start and finish but a condition that establishes its finish. First, let’s watch their syntax possibles:
do [while or until condition]
... statements ...
loop
Always starts with do and finishes with loop. While and until are not mandatory but they’re used to manage the condition and establish the end of loop. In the block we put all the statements that we want to repeat. Let’s watch the other syntax:
do
... statements ...
loop [while or until condition]
It’s exactly the same but with the condition at the end. This had importance with old computers because on this way it procceced the data before or after the condition but in the actual times this doesn’t matter. Let’s watch an example:
dim text, value, number
number = 1
do
text="Loop: " & number & vbcrlf & "Do you want to continue?"
value = msgbox(text,4,"do sample")
number = number + 1
loop while value = 6
msgbox "See ya next time",,"do sample"
We declare three variables and start number with an initial value. Then, we’ve the loop where define a text to show. This text is used for a msgbox where we use the value 4 to establish the buttons Ok and Cancel and in value store the pressed button. In this case, if we press Ok returns 6 and 7 for Cancel. The next is increment to number that we use to represent the number of lap. The loop is realized while value has 6 else it outs and show the final message. Let’s watch how it works:
Note:
If you want to know more about msgbox, in this post we talked about it.
In the video, we can watch how it repeats the loop while Cancel is not pressed. THat’s why is denominated as infinite. Because of this behaviour is usually used to make the life cycle of a program. Let’s take the above example and modify it:
dim text, value, number
number = 1
do
text="Loop: " & number & vbcrlf & "Do you want to continue?"
value = msgbox(text,4,"do sample")
number = number + 1
if value = 7 then exit do
loop
msgbox "See ya next time",,"do sample"
It’s very similar to the other code but we take out the condition in loop and we add a conditional in the block. This conditional ends the loop when value is equal to 7 (Cancel button) with the statement exit do. This is used when there is no a condition to end the loop. This works in the same way but this time expects the Cancel button instead of Ok button. At the same that the conditions, exit do is optional too. We can use it directly but it’s usually most used with a conditional.
In summary, we talked about do…loop, what it is, how it works, an example with a practical use, and another syntax for this statement. I hope you’ve found it useful. You can follow me on this social networks:


Donatión
It’s for site maintenance, thanks!
$1.50
