(→Example) |
(→Example) |
||
Line 37: | Line 37: | ||
− | + | Example of a simple counter to count the number of iterations of a loop | |
<syntaxhighlight lang="mIRC"> | <syntaxhighlight lang="mIRC"> | ||
;Count the number of iterations from 1 to 5 | ;Count the number of iterations from 1 to 5 |
Revision as of 03:48, 1 September 2025
The /while command is a construct in the mIRC scripting language that can perform repetitive operations. The block of statements and commands inside the body of the while loop will be executed as long as the condition is true. You can manually break out of a loop by using the Cltr+Break key combinations.
Synopsis
while (condition) { statements or commands statements or commands statements or commands }
Switches
None
Parameters
None
Example
Basic construct
;Create a counter variable, set its value to 1 var %i 1 ;Create a stop variable with a value equal to the number at which you want to stop the loop. var %stop 5 ;Start of loop with a condition in round brackets () - work while the number in the variable %i is less than or equal to the number in the variable %stop while (%i <= %stop) { ;Print echo message to active window with value of counter variable echo -a Number in counter: %i ;Increase the counter variable by +1 inc %i }
Note: This code snippet should be used inside an alias or script.
Example of a simple counter to count the number of iterations of a loop
;Count the number of iterations from 1 to 5 alias whilecounter { var %i 1, %stop 5 while (%i <= %stop) { echo -a Number in counter: %i inc %i } } ;To call the alias use the command: /whilecounter
The above example will output:
Number in counter: 1 Number in counter: 2 Number in counter: 3 Number in counter: 4 Number in counter: 5
Example of reading a file by iterating over each line
;Read all lines of a file alias whilereadfile { var %i 1 | while (%i <= $lines(file.txt)) { echo -a line %i - $read(file.txt,nt,%i) inc %i } } ;To call the alias use the command: /whilereadfile ;Read from line 5 and stop at line 10 alias whilereadfile2 { var %i 5 | while (%i <= $lines(file.txt)) { echo -a line %i - $read(file.txt,nt,%i) if (%i == 10) break inc %i } } ;To call the alias use the command: /whilereadfile2
Example of how to print a list all variable names with their values
alias whilelistvars { ;Check if there are any variables set if (!$var(*,0)) { echo -a There Are No Variables. ;Return command to terminate alias function return } ;Set a counter variable var %x 1 ;Loop while the counter variable is less than or equal to the total number of variables while (%x <= $var(*,0)) { ;Print out the variable and its value echo -a %x $+ ) $var(*,%x) = $var(*,%x).value ;Increase the variable by one inc %x } } ;To call the alias use the command: /whilelistvars
The above example will output something like this:
1) %Var = Value 2) %Foo = FooBar 3) %Bar = BarFoo
Compatibility
Added: mIRC v5.7
Added on: 02 Feb 2000
Note: Unless otherwise stated, this was the date of original functionality.
Further enhancements may have been made in later versions.