For...Next statement - MapBasic - 2023

MapInfo MapBasic Reference

Product type
Software
Portfolio
Locate
Product family
MapInfo
Product
MapInfo > MapBasic
Version
2023
Language
English
Product name
MapBasic
Title
MapInfo MapBasic Reference
First publish date
1985
Last updated
2023-09-12
Published on
2023-09-12T16:32:32.686312

Purpose

Defines a loop which will execute for a specific number of iterations.

Restrictions

You cannot issue a For...Next statement through the MapBasic window.

Syntax

For var_name = start_expr To end_expr [ Step inc_expr ] 
	 statement_list 
Next 

var_name is the name of a numeric variable.

start_expr is a numeric expression.

end_expr is a numeric expression.

inc_expr is a numeric expression.

statement_list is the group of statements to execute with each iteration of the For loop.

Description

The For...Next statement provides loop control. This statement requires a numeric variable (identified by the var_name parameter). A For...Next statement either executes a group of statements (the statement_list) a number of times, or else skips over the statement_list completely. The start_expr, end_expr, and inc_expr values dictate how many times, if any, the statement_list will be carried out.

Upon encountering a For...Next statement, MapBasic assigns the start_expr value to the var_name variable. If the variable is less than or equal to the end_expr value, MapBasic executes the group of statements in the statement_list, and then adds the inc_expr increment value to the variable. If no Step clause was specified, MapBasic uses a default increment value of one. MapBasic then compares the current value of the variable to the end_expr expression; if the variable is currently less than or equal to the end_expr value, MapBasic once again executes the statements in the statement_list. If, however, the var_name variable is greater than the end_expr, MapBasic stops the For loop, and resumes execution with the statement which follows the Next statement.

Conversely, the For...Next statement can also count downwards, by using a negative Step value. In this case, each iteration of the For loop decreases the value of the var_name variable, and MapBasic will only decide to continue executing the loop as long as var_name remains greater than or equal to the end_expr.

Each For statement must be terminated by a Next statement. Any statements which appear between the For and Next statements comprise the statement_list; this is the list of statements which will be carried out upon each iteration of the loop.

The Exit For statement allows you to exit a For loop regardless of the status of the var_name variable. The Exit For statement tells MapBasic to jump out of the loop, and resume execution with the first statement which follows the Next statement.

MapBasic permits you to modify the value of the var_name variable within the body of the For loop; this can affect the number of times that the loop is executed. However, as a matter of programming style, you should try to avoid altering the contents of the var_name variable within the loop.

Example

Dim i As Integer

' the next loop will execute a Note statement 5 times
For i = 1 to 5 
	Note "Hello world!"
Next

' the next loop will execute the Note statement 3 times
For i = 1 to 5 Step 2
	Note "Hello world!" 
Next

' the next loop will execute the Note statement 3 times
For i = 5 to 1 Step -2 
	Note "Hello world!" 
Next

' MapBasic will skip the following For statement
' completely, because the initial start value is
' already larger than the initial end value
For i = 100 to 50 Step 5
	Note "This note will never be executed"
Next

See Also:

Do...Loop statement, Exit For statement