Loops

RCBasic provides three types of loops: For, While, and Do.

For Loop

Repeats a block a fixed number of times, incrementing a counter variable each iteration.

For I = 1 To 5
Print I
Next
' Output: 1 2 3 4 5

Use Step to change the increment amount:

For I = 1 To 5 Step 2
Print I
Next
' Output: 1 3 5

Step can also be negative to count down:

For I = 10 To 1 Step -1
Print I
Next

While Loop

Executes a block of code while a condition remains true. The condition is tested before each iteration, so the body may never execute if the condition is false from the start.

I = 0
While I < 5
Print I
I = I + 1
Wend
' Output: 0 1 2 3 4

Warning

Make sure the loop variable changes inside the loop body, otherwise you will create an infinite loop.

Do Loop

Similar to While, but the condition is checked after the body executes. This guarantees the body runs at least once.

The Do loop has three forms:

Infinite loop

Do
Print "HELLO WORLD"
Loop
' Runs forever — use Exit to break out.

Loop While (continue while true)

I = 0
Do
Print I
I = I + 1
Loop While I < 5
' Output: 0 1 2 3 4

Loop Until (continue until true)

I = 0
Do
Print I
I = I + 1
Loop Until I = 5
' Output: 0 1 2 3 4

Summary

Loop

Condition checked

Best used when

For

Start (count-based)

You know the exact number of iterations.

While

Start

You need to check a condition before the first run.

Do

End

The body must always execute at least once.