Computer Science

VB.NET Loops Visual Recap

Estimated reading: 2 minutes 93 views Contributors

VB.NET Loops — Visual Recap & Quick Guide

Tip: Click Copy on any snippet to paste into Visual Studio or LINQPad.

For…Next

Best when you know the exact iteration count. Supports positive/negative Step.

For i As Integer = 1 To 10
    Console.WriteLine(i)
Next
For i As Integer = 10 To 1 Step -1
    Console.WriteLine(i)
Next

Flow (mental model)

  • Init counter → i = start
  • Check boundary → i <= end?
  • If True → run body → i += Step → repeat
  • If False → exit loop
Bounds can be variables (e.g., For i = startNum To endNum).

For Each…Next

Iterate collections and arrays. The iteration variable is read‑only.

Dim fruits As String() = {"Apple", "Banana", "Cherry"}
For Each fruit As String In fruits
    Console.WriteLine(fruit)
Next
Dim nums As New List(Of Integer)({1,2,3,4})
For Each n In nums
    Console.WriteLine(n)
Next

Flow

  • Get enumerator → MoveNext()
  • If True → Current item → body
  • Repeat until MoveNext() returns False
Use LINQ‑style actions (advanced)
Dim numbers = {1,2,3,4,5}
numbers.ToList().ForEach(Sub(n) Console.WriteLine(n))

Do While / Do Until (pre‑check)

Condition is checked first. May execute the body zero or more times.

Dim c As Integer = 0
Do While c < 5
    Console.WriteLine(c)
    c += 1
Loop
Dim c As Integer = 0
Do Until c >= 5
    Console.WriteLine




Share this Doc

VB.NET Loops Visual Recap

Or copy link

CONTENTS