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