Lesson 15: Looping in VB2019
Master repetitive execution with efficient loop structures
Key Takeaway
Looping allows you to execute code repeatedly, enabling efficient processing of repetitive tasks and data sets.
In this lesson, we'll explore looping structures in Visual Basic 2019. Loops are essential for executing code repeatedly without writing redundant statements, making them perfect for processing collections, generating sequences, and automating repetitive tasks.
15.1 Loop Structures
Visual Basic 2019 provides three primary looping structures: For..Next, Do..Loop, and While..End While. Each serves different purposes in controlling repetitive execution.
Loop Comparison
Loop Type | Usage | Best For |
---|---|---|
For..Next | Fixed number of iterations | Processing arrays, known ranges |
Do..Loop | Condition-based execution | User input validation, file reading |
While..End While | Condition-based execution | Processing until condition changes |
15.1(a) For..Next Loop
The For..Next loop executes a block of code a specific number of times. It's ideal when you know exactly how many iterations you need.
' Code to execute
Next
Example 15.1: Number Sequence
This program displays numbers from 1 to 10 in a list box.
Private Sub BtnGenerate_Click(sender As Object, e As EventArgs) Handles BtnGenerate.Click Dim counter As Integer For counter = 1 To 10 ListBox1.Items.Add(counter) Next End Sub
Output:
Example 15.2: Step Increment
This example demonstrates using step increments to generate even numbers.
Private Sub BtnEvenNumbers_Click(sender As Object, e As EventArgs) Handles BtnEvenNumbers.Click Dim counter As Integer For counter = 0 To 20 Step 2 ListBox1.Items.Add(counter) Next End Sub
Output:
15.1(b) Do Loops
Do loops execute while or until a condition is met, making them ideal for situations where the number of iterations is unknown.
' Code to execute
Loop
Example 15.3: Input Validation
This program repeatedly prompts for input until the user enters a positive number.
Private Sub BtnValidate_Click(sender As Object, e As EventArgs) Handles BtnValidate.Click Dim userInput As Integer Do userInput = Val(InputBox("Enter a positive number:")) Loop Until userInput > 0 LblResult.Text = "You entered: " & userInput End Sub
Output:
15.1(c) While..End While
The While..End While loop executes as long as a specified condition remains true.
' Code to execute
End While
Example 15.4: Countdown Timer
This example creates a countdown from 10 to 1.
Private Sub BtnCountdown_Click(sender As Object, e As EventArgs) Handles BtnCountdown.Click Dim count As Integer = 10 While count >= 1 ListBox1.Items.Add(count) count -= 1 End While ListBox1.Items.Add("Liftoff!") End Sub
Output:

15.1(d) Exit Statements
Exit statements allow you to break out of loops prematurely when certain conditions are met.
Exit Do - Breaks out of Do loop
Exit While - Breaks out of While loop
Example 15.5: Prime Number Check
This example checks if a number is prime using an efficient loop.
Private Sub BtnCheckPrime_Click(sender As Object, e As EventArgs) Handles BtnCheckPrime.Click Dim num As Integer = Val(TxtNumber.Text) Dim isPrime As Boolean = True If num <= 1 Then isPrime = False Else For i = 2 To Math.Sqrt(num) If num Mod i = 0 Then isPrime = False Exit For End If Next End If If isPrime Then LblResult.Text = num & " is a prime number" Else LblResult.Text = num & " is not a prime number" End If End Sub
Output:
Lesson Summary
In this lesson, you've learned how to use looping structures to execute code repeatedly in Visual Basic 2019:
For..Next Loop
Mastered fixed iteration loops with step increments
Do Loops
Learned condition-based loops for unknown iterations
While..End While
Implemented top-condition checking loops
Exit Statements
Used Exit For/Do/While to break loops early
Looping structures are fundamental for efficient programming, allowing you to process collections, validate input, and perform repetitive tasks with minimal code. In the next lesson, we'll explore sub procedures to organize and modularize your code.
Next Lesson
Ready to learn about organizing your code? Continue to Lesson 16: Sub Procedures.
Related Resources

Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.

Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.