- VB.Net Basic Tutorial
- VB.Net - Home
- VB.Net - Overview
- VB.Net - Environment Setup
- VB.Net - Program Structure
- VB.Net - Basic Syntax
- VB.Net - Data Types
- VB.Net - Variables
- VB.Net - Constants
- VB.Net - Modifiers
- VB.Net - Statements
- VB.Net - Directives
- VB.Net - Operators
- VB.Net - Decision Making
- VB.Net - Loops
- VB.Net - Strings
- VB.Net - Date & Time
- VB.Net - Arrays
- VB.Net - Collections
- VB.Net - Functions
- VB.Net - Subs
- VB.Net - Classes & Objects
- VB.Net - Exception Handling
- VB.Net - File Handling
- VB.Net - Basic Controls
- VB.Net - Dialog Boxes
- VB.Net - Advanced Forms
- VB.Net - Event Handling
- VB.Net Advanced Tutorial
- VB.Net - Regular Expressions
- VB.Net - Database Access
- VB.Net - Excel Sheet
- VB.Net - Send Email
- VB.Net - XML Processing
- VB.Net - Web Programming
- VB.Net Useful Resources
- VB.Net - Quick Guide
- VB.Net - Useful Resources
- VB.Net - Discussion
VB.Net - For...Next Loop
It repeats a group of statements a specified number of times and a loop index counts the number of loop iterations as the loop executes.
The syntax for this loop construct is −
For counter [ As datatype ] = start To end [ Step step ] [ statements ] [ Continue For ] [ statements ] [ Exit For ] [ statements ] Next [ counter ]
Flow Diagram
Example
Module loops Sub Main() Dim a As Byte ' for loop execution For a = 10 To 20 Console.WriteLine("value of a: {0}", a) Next Console.ReadLine() End Sub End Module
When the above code is compiled and executed, it produces the following result −
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19 value of a: 20
If you want to use a step size of 2, for example, you need to display only even numbers, between 10 and 20 −
Module loops Sub Main() Dim a As Byte ' for loop execution For a = 10 To 20 Step 2 Console.WriteLine("value of a: {0}", a) Next Console.ReadLine() End Sub End Module
When the above code is compiled and executed, it produces the following result −
value of a: 10 value of a: 12 value of a: 14 value of a: 16 value of a: 18 value of a: 20
vb.net_loops.htm
Advertisements