RCBasic Loops [RCBasic Doc]

## LOOPS

RC BASIC has 3 types of loops: FOR, WHILE, and DO.

The FOR loop repeats a block of code and increments a counter each time it finishes the block between FOR and NEXT. When the counter reaches a certain value the FOR loop stops.

`For I = 1 To 5
Print I
Next
`

The above code will output the numbers 1 to 5 to the console. Optionally you could use the STEP keyword to set the number that the counter will increase by. Look at the following:

`For I = 1 To 5 Step 2
Print I
Next
`

The above code will output the numbers 1, 3, and 5 to the console. The STEP keyword increases I by 2 each time through the FOR loop.

WHILE loops will execute a block of code while a certain condition is true.

`I = 0
While I < 5
Print I
I = I + 1
Wend
`

The above code will output the numbers 0 to 4. It will not output the number 5 because if ( I < 5 ) is false the loop will not repeat.

The DO loop is similar to the WHILE loop with an exception. The WHILE loop checks for the loop condition at the start of the loop but the DO loop checks for the loop condition at the end of the loop. What this means is that a WHILE loop will never execute if the condition is false at the start but the DO loop is guaranteed to execute at least once before it checks if the condition was true or not.

The DO loop is also unique in that it has 3 different forms. Here is the simplest form of the DO loop.

`Do
Print "HELLO WORLD"
Loop
`

The code above will continue to output HELLO WORLD to the console infinitely.

`I = 0
Do
Print I
I = I + 1
Loop While I < 5
`

The above code will output the numbers 0 to 4 to the console. This form of the DO loop will continue to loop while the loop condition is true.

`I = 0
Do
Print I
I = I + 1
Loop Until I = 5
`

The above code will output the numbers 0 to 4 to the console. This form of the DO loop will continue to loop until the loop condition is true.