O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

For...Next Statement

Syntax

For counter = initial_value To maximum_value _
             [Step stepcounter]
   'code to execute on each iteration
   [Exit For]
Next [counter]
counter (required (optional with Next statement); any valid numeric variable)

A variable that serves as the loop counter

initial_value (required; any valid numeric expression)

The starting value of counter for the first iteration of the loop

maximum_value (required; any valid numeric expression)

The value of counter during the last iteration of the loop

stepcounter (optional (required if Step is used); any valid numeric expression)

The amount by which counter is to be incremented or decremented on each iteration of the loop

Description

Defines a loop that executes a given number of times, as determined by a loop counter.

To use the For...Next loop, you must assign a numeric value to a counter variable. This counter is either incremented or decremented automatically with each iteration of the loop. In the For statement, you specify the value that is to be assigned to the counter initially and the maximum value the counter will reach for the block of code to be executed. The Next statement marks the end of the block of code that is to execute repeatedly, and it also serves as a kind of flag that indicates that the counter variable is to be modified.

Rules at a Glance

  • If maximum_value is greater than initial_value and no Step keyword is used or the step counter is positive, the For...Next loop is ignored and execution commences ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required