ZetCode

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.

basic_for.bas
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_step.bas
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.

descending_for.bas
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.

nested_for.bas
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.

exit_for.bas
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.

float_for.bas
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.

multi_var_for.bas
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

This tutorial covered the FreeBasic For/To/Step/Next keywords with practical examples showing various loop patterns and techniques.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.