Loops in Visual Basic
For, Step, Do While and Do Until — every loop VB owns.
For lap As Integer = 1 To 3
Console.WriteLine($"lap {lap}")
Next
For n = 10 To 0 Step -5
Console.Write($"{n} ")
Next
Console.WriteLine()
Dim fuel = 5
Do While fuel > 0
fuel -= 2
Loop
Console.WriteLine($"fuel left: {fuel}")
Dim tries = 0
Do
tries += 1
Loop Until tries = 3
Console.WriteLine($"took {tries} tries")
How it works
For ... Tocounts inclusively;Step -5runs it backwards.Do Whiletests before the body; the loop may never run.Do ... Loop Untiltests after — it always runs at least once.
Keywords and builtins used here
DimDoForIntegerLoopNextStepToWhile
The run, in numbers
- Lines
- 20
- Characters to type
- 315
- Tokens
- 86
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 57 seconds.
Step 3 of 3 in Control flow, step 9 of 29 in Language basics.