typestar

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

  1. For ... To counts inclusively; Step -5 runs it backwards.
  2. Do While tests before the body; the loop may never run.
  3. Do ... Loop Until tests after — it always runs at least once.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Control flow, step 9 of 29 in Language basics.

← Previous Next →

Loops in other languages