While loops
While loops
While loops allow you to repeat a set of commands while the expression inside the brackets is $true.
while (expression) { commands }
The expression is evaluated in the same manner as If-Then-Else expressions are evaluted. If the expression is $true the commands inside the brackets are run while the expression remains $true.
You can break out of the while loop by issuing the break command, or jump to the start of the while loop by issuing the continue command.
Every time the while loop jumps back to the top, the expression is revaluated.
Note: If the expression evaluation changes to $false during the while loop you will not break out of the while loop until the while loop has completed.
Examples
Example shows how to get a list of users in the channel channel:
var %i = $chan(#,0) while (%i) { var %people = $addtok(%people,$chan(#,%i)),44) dec %i } echo -ag Current channel users: %people
Example shows how to loop though from 1 to 50:
var %from = 1 var %to = 50 while (%from <= %to) { echo -ag I know I can count to %from inc %from }
Example shows how to use the break command:
var %i = 1 while (%i) { echo -ag $chr(37) $+ i = %i if (%i == 10) { break } inc %i }
Example shows how to use the continue command. It will echo everything from 1 to 10 except 5, as it jumps to the begin of the loop before coming to the echo command.
var %i = 0 while (%i < 10) { inc %i if (%i == 5) { continue } echo -ag $chr(37) $+ i == %i }