ZetCode

FreeBasic For/To/Step/Next Keywords

last modified June 16, 2025

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
For i As Integer = 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. 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 counts from 0.0 to 1.0 in 0.1 increments. Floating-point loops require care due to precision issues. The actual iterations may vary slightly from expected due to floating-point representation.

For Loop With Multiple Variables

FreeBasic allows declaring multiple loop variables.

multi_var_for.bas
For i As Integer = 1, j As Integer = 10 To 1 Step -1
    Print "i:"; i; " j:"; j
Next i, j

This example shows two counters moving in opposite directions. The comma separates variable declarations. Each Next statement must list the variables in reverse order of their declaration.

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.