FreeBasic For/To/Step/Next Keywords
Last modified October 18, 2026
The FreeBasic For/To/Step/Next keywords create loops that execute
a block of code a specific number of times. These loops are essential for
iterating over ranges and collections efficiently.
Basic Definition
In FreeBasic, For starts a loop with a counter variable. To
specifies the end value, Step sets the increment, and Next
marks the loop's end and updates the counter.
The loop executes while the counter is within bounds. The default step is 1 if omitted. For loops are deterministic and ideal when iteration count is known.
Basic For Loop
This example demonstrates the simplest form of a For loop.
For i As Integer = 1 To 5
Print "Iteration: "; i
Next
This loop prints numbers 1 through 5. The counter i starts at 1,
increments by 1 each iteration, and stops after reaching 5. The loop body
executes exactly 5 times with predictable values.
For Loop With Step
The Step keyword controls the loop's increment value.
For i As Integer = 0 To 10 Step 2
Print "Even number: "; i
Next
This loop prints even numbers from 0 to 10. The Step 2 makes the counter increase by 2 each iteration. Step can be positive or negative, allowing flexible iteration patterns.
Descending For Loop
Negative Step values create descending loops.
For i As Integer = 10 To 1 Step -1
Print "Countdown: "; i
Next
Print "Blast off!"
This example counts down from 10 to 1. The negative Step makes the counter decrease each iteration. The loop stops when the counter passes the end value, showing how bounds work with negative steps.
Nested For Loops
For loops can be nested to handle multi-dimensional iterations.
For row As Integer = 1 To 3
For col As Integer = 1 To 3
Print "("; row; ","; col; ") ";
Next
Print
Next
This creates a 3x3 grid output. The outer loop handles rows, while the inner loop handles columns. Each complete inner loop iteration happens for every outer loop iteration.
For Loop With Exit
Loops can be exited prematurely using Exit For.
Dim i As Integer
For i = 1 To 100
Print i;
If i >= 10 Then
Exit For
End If
Next
Print "Loop exited at"; i
This loop would normally run to 100, but exits early when i reaches 10.
The counter is declared before the loop with Dim because a variable
declared with As in the For statement is local to the loop and
cannot be used after Next. The output is
1 2 3 4 5 6 7 8 9 10Loop exited at 10.
Exit For immediately terminates the loop, useful for search operations or
error conditions.
Floating Point For Loop
For loops can use floating-point counters with caution.
For x As Single = 0.0 To 1.0 Step 0.1
Print "x ="; x
Next
This loop is meant to count from 0.0 to 1.0 in 0.1 increments, but it prints ten values, not eleven. The last value printed is 0.9000001, and 1.0 is never reached because the Single values accumulate rounding error. Floating-point loops require care due to precision issues.
For Loop With Multiple Variables
FreeBasic does not allow several loop counters in one For statement. Counters that must be stepped together are handled with a single loop and a second variable updated in the body.
For i As Integer = 1 To 10
Dim j As Integer = 11 - i
Print "i:"; i; " j:"; j
Next
This example shows two counters moving in opposite directions. The second
counter j is computed from i inside the loop body.
Writing For i As Integer = 1, j As Integer = 10 To 1 Step -1
or Next i, j does not compile in FreeBasic.
Best Practices
- Scope: Declare loop variables in the For statement when possible.
- Step: Always specify Step explicitly for clarity.
- Bounds: Ensure loop bounds are correct for the Step direction.
- Modification: Avoid modifying loop counters within the body.
- Performance: Use integer counters when possible for speed.
This tutorial covered the FreeBasic For/To/Step/Next keywords with
practical examples showing various loop patterns and techniques.
Author
List all FreeBasic Tutorials.