mIRC Scripting

mIRC: While Loops

A while loop statement is used to execute a block of code multiple amounts of time. The code inside the while loop will get executed as long as the condition is $TRUE (see /IF Statements).

The basic structure of a while loop is:

While (<condition>) {
  /*
    Code here will be executed as long as
    the condition is $TRUE
  */

}

Since while loops keep executing the code as long as the condition is true, sometimes an infinite loop can occur, in such cases the code will get executed endlessly. You can manually break the code using the Ctrl+Break (sometimes ‘Pause’) short cut key.

Back References

mIRC offers two identifiers to retrieve the first or second parameter of the while statement. Please note, the identifierswill return the first and second parameter of the $TRUE condition.

$v1 and $v2

In the example below we will count from 1 to 10 using a while loop. Variable “%a” will be set to 1, the loop will keep executing as long as %a is less than or equal to 10. We will use /inc %a to increase %a by a value of one every loop.

Alias Count_To_Ten {
  var %a 1
  while (%a <= 10) {
    echo -a Count: $v1
    inc %a
  }
}

Example

In the example below we will check in what channels a nick is on with us. We use $comchan(<user>,0) to give us the total count.

Alias WithMe {
  if (!$1) { echo 2 -ea * /WithMe: insufficient parameters | halt }
  if (!$comchan($1,0)) echo -a User is not in any of your channels!
  else {
    ;our counter, and list
    var %c 1, %l
    /*
      Keep looping as long as the counter
      is less than or equal to the total
    */

    while (%c <= $comchan($1,0)) {
      ;Add the channel name to the list of channels.
      var %l %l $comchan($1,%c)
      ;Increase the counter by 1
      inc %c
    }
    echo -a User: $1
    /*
      The $v2 comes from the while loop’s conditional statement.
      %c <= $comchan($1,0)

      $v1 would be the counter which will return $comchan($1,0) + 1
      since it will increase by one on its last loop

      $v2 is $comchan($1,0)
    */

    echo -a Total Channels With You: $v2
    echo -a Channels: %l
  }
}