For...Next
 
Control flow statement for looping

Syntax

For iterator [ As datatype ] = startvalue To endvalue [ Step stepvalue ]
[ statement block ]
Next [ iterator ]

Parameters

iterator
a variable identifier that is used to iterate from an initial value to an end value
datatype
If specified, the variable iterator will automatically be declared with the type datatype.
startvalue
an expression that denotes the starting value of the iterator
endvalue
an expression used to compare with the value of the iterator
stepvalue
an expression that is added to the iterator after every iteration

Description

A For...Next loop initializes iterator to startvalue, then executes the statement block's, incrementing iterator by stepvalue until it reaches endvalue. If stepvalue is not explicitly given it will set to 1.

The values of stepvalue and endvalue are stored internally immediately following execution of the For statement and thus neither can be changed inside the For loop. (the variables that define them can be changed, but changing those variables inside the For loop will not affect its execution. See examples)

The iterator may be defined having the same scope as the For statement by using the As datatype syntax. With this syntax, iterator is created and destroyed within the For...Next scope. See dialect differences below.

If endvalue is less than startvalue then a negative stepvalue must be specified or the statement block will not execute at all, since startvalue compares greater than endvalue.

The For statement causes the execution of the statements in the statement block until iterator compares greater than endvalue (or less than endvalue if stepvalue < 0). iterator will be incremented the amount of stepvalue following each execution of the statement block. If an increment is not given, iterator will be implicitly incremented by 1.

If an Exit For statement is encountered inside the statement block, the loop is terminated, and execution resumes immediately following the enclosing Next statement. If a Continue For statement is encountered, the rest of the statement block is skipped and execution resumes at the FOR statement.

Like all control flow statements, the For statement can be nested, that is, it can be used in a statement block of another For statement.

note: When a negative stepvalue is specified, the For statement loops until iterator compares less than end_value.

For, Next, and Step are operators that can be overloaded inside user defined types. See Operator For, Operator Next, Operator Step

Example

Dim i As Single
Print "counting from 3 to 0, with a step of -0.5"
Dim i As Single
For i = 3 To 0 Step -0.5
    Print "i is " & i
Next i

Dim As Integer i, j, k, toTemp, stepTemp
j = 9: k = 1

For i = 0 To j Step k
    
    j = 0: k = 0 '' Changing j and k has no effect on the current loop.
    Print i;
    
Next i
Print

' Internally, this is what the above example does:
j = 9: k = 1

i = 0: toTemp = j: stepTemp = k
Do While IIf(stepTemp >= 0, i <= toTemp, i >= toTemp)
    
    j = 0: k = 0 '' Changing j and k has no effect on the current loop.
    Print i;
    
    i += stepTemp
Loop
Print


Dialect Differences

  • In the -lang qb and -lang fblite dialects, variables declared inside a FOR..NEXT loop have a function-wide scope as in QB
  • In the -lang fb and -lang deprecated dialects, variables declared inside a For..Next block are visible only inside the block, and can't be accessed outside it.

Differences from QB

  • ByRef arguments cannot be used as counters.

See also

Сайт ПДСНПСР. Если ты патриот России - жми сюда!


Знаете ли Вы, что только в 1990-х доплеровские измерения радиотелескопами показали скорость Маринова для CMB (космического микроволнового излучения), которую он открыл в 1974. Естественно, о Маринове никто не хотел вспоминать. Подробнее читайте в FAQ по эфирной физике.

НОВОСТИ ФОРУМАФорум Рыцари теории эфира
Рыцари теории эфира
 10.11.2021 - 12:37: ПЕРСОНАЛИИ - Personalias -> WHO IS WHO - КТО ЕСТЬ КТО - Карим_Хайдаров.
10.11.2021 - 12:36: СОВЕСТЬ - Conscience -> РАСЧЕЛОВЕЧИВАНИЕ ЧЕЛОВЕКА. КОМУ ЭТО НАДО? - Карим_Хайдаров.
10.11.2021 - 12:36: ВОСПИТАНИЕ, ПРОСВЕЩЕНИЕ, ОБРАЗОВАНИЕ - Upbringing, Inlightening, Education -> Просвещение от д.м.н. Александра Алексеевича Редько - Карим_Хайдаров.
10.11.2021 - 12:35: ЭКОЛОГИЯ - Ecology -> Биологическая безопасность населения - Карим_Хайдаров.
10.11.2021 - 12:34: ВОЙНА, ПОЛИТИКА И НАУКА - War, Politics and Science -> Проблема государственного терроризма - Карим_Хайдаров.
10.11.2021 - 12:34: ВОЙНА, ПОЛИТИКА И НАУКА - War, Politics and Science -> ПРАВОСУДИЯ.НЕТ - Карим_Хайдаров.
10.11.2021 - 12:34: ВОСПИТАНИЕ, ПРОСВЕЩЕНИЕ, ОБРАЗОВАНИЕ - Upbringing, Inlightening, Education -> Просвещение от Вадима Глогера, США - Карим_Хайдаров.
10.11.2021 - 09:18: НОВЫЕ ТЕХНОЛОГИИ - New Technologies -> Волновая генетика Петра Гаряева, 5G-контроль и управление - Карим_Хайдаров.
10.11.2021 - 09:18: ЭКОЛОГИЯ - Ecology -> ЭКОЛОГИЯ ДЛЯ ВСЕХ - Карим_Хайдаров.
10.11.2021 - 09:16: ЭКОЛОГИЯ - Ecology -> ПРОБЛЕМЫ МЕДИЦИНЫ - Карим_Хайдаров.
10.11.2021 - 09:15: ВОСПИТАНИЕ, ПРОСВЕЩЕНИЕ, ОБРАЗОВАНИЕ - Upbringing, Inlightening, Education -> Просвещение от Екатерины Коваленко - Карим_Хайдаров.
10.11.2021 - 09:13: ВОСПИТАНИЕ, ПРОСВЕЩЕНИЕ, ОБРАЗОВАНИЕ - Upbringing, Inlightening, Education -> Просвещение от Вильгельма Варкентина - Карим_Хайдаров.
Bourabai Research Institution home page

Боровское исследовательское учреждение - Bourabai Research Bourabai Research Institution