MIRC Scripting/BeyondBasics/Loops

Chapter 3: Branching and Loops 25% developed  as of 14:23, 31 March 2008 (UTC)


Getting Started mIRC Scripting 25% developed  as of March 13, 2008 Timers

Starting From Scratch: Become Familiar with mIRC | Getting Started
Learning the Ropes: Branching and Loops | Timers | User Levels | Text Matching | File Handling | Hash Tables | Dialogs
Advanced Subjects: Custom Windows | Agents | Sockets
Planned: DLLs | COM Objects | Error Checking and Handling



Loops and branching are more mIRC scripting basics that will come up in almost every script you make. They help control the flow of your script and tell it what to do. Think of them as a tour guide in a museum, or a factory overseer in a factory. The tour guide is like an if statement, and the factory overseer is like while loop. There is also a third kind of statement, using a command called /goto that can act as either kind.

If-Then-Else edit

You will use if, elseif, and else more than any of the other statements in this section. Without these commands to tell the script where to go and what to do, your script would follow one linear path to the end every time. If statements can also be used for error checking, to make sure the right conditions exist for the script to run.

On *:Text:!*:#:{
  if ($1 == !1) { msg $chan You picked the number 1, $nick $+ ! }
  elseif ($1 == !2) { msg $chan You picked the number 2, $nick $+ ! }
  else { msg $chan Your number was not 1 or 2, $nick $+ . }
}

This is an example of a remote script that uses if statements for branching. The first statement checks if $1 equals !1. If it does, it will message the channel and stop. If not, it will move on to the next statement, the elseif. This statement activates only if the if statement fails. It checks to see if $1 equals !2. If it does, it will message the channel. Otherwise, it will move to the last resort, the else statement. This will message the channel the number was not 1 or 2.

On *:Text:!*:#:{
  if ($2) {
    if ($2 == 1) { msg $chan You picked the number 1, $nick $+ ! }
    elseif ($2 == !2) { msg $chan You picked the number 2, $nick $+ ! }
    else { msg $chan Your number was not 1 or 2, $nick $+ . }
    return
  }
  else { msg $chan You need to say ! and then a number. }
}

This script demonstrates three important things. The first is the ability to check for errors. The first if statement checks that there are two parameters. The second thing shown is a nested if statement. The if/elseif/else this time are nested within the first If statement. These statements only evaluate if the If containing them is true. They then evaluate as normal. The last thing demonstrated is the /return command, which will be covered below shortly. It stops the script if the first if statement is true to prevent the error from appearing.

If on the command line edit

If statements can be used on the command line when double prefixed (//).

//if (1 = 1) { echo -a True! }

This will evaluate the if statement and do the command inside the brackets. Note that you need to use pipes to do multiple commands on the command line.

//if (1 = 1) { echo -a True! | echo -a 1 does equal 1! }

Cancelling Operators edit

In an if-then-else statement, $iif identifier, or while loop, you can use the ! prefix to mean NOT. For example, if ($1 != 1) would mean "If $1 is NOT equal to 1."

$v1 and $v2 edit

$v1 and $v2 are identifier that return the first and second conditions in an if-then-else, $iif, or while command. In the statement if (5 == 3), $v1 would return 5, and $v2 would equal 3.

$iif edit

$iif is the identifier version of an if command. The syntax of the identifier is:

$iif(c1 operator c2,true,false)

c1, c2, and operator all mean the same thing as in an if statement. c1 and c2 are the conditions that the statement checks, and the operator is what the conditions are compared with.

//echo -a $iif($?="Pick a number" == $?="Pick another number",$true,$false)

Enter that command in the command line. You should be prompted for two numbers. If the numbers you enter are equal, the command will echo $true to your screen. Otherwise, the command will echo $false to your screen.

While Loops edit

Remember the factory overseer analogy I made before? Well, this is how it applies itself. A while loop simply repeats a set of commands over and over until the conditions provided are no longer true. Think of it as a repeating if statement.

while (conditions) {
  commands
}

This is the syntax for a while loop. Notice how it looks a lot like an if statement? The difference here is that while loops repeat until the conditions are false.

On *:Text:!channels:#:{
  var %chan = 1
  while (%chan <= $chan(0)) {
    var %chans = %chans $chan(%chan)
    inc %chan
  }
  msg $chan I am on %chans $+ , $nick $+ .
}

The function of this loop is to let someone type !channels in a channel, and you will report back what channels you are on. The factory overseer is a metaphor for someone in a work environment telling a worker to do a job until they are done for the day. The while loop is the worker, performing its "task" until the conditions are no longer true. In this case, while the variable %chan is less than or equal to the number of channels you are on ($chan(0)) it will add the channel name to the variable %chans and then increase %chan. Once %chan is greater than $chan(0) it will stop and proceed with the next command.


On *:Text:!channels:#:{
  var %chan = 1
  while (%chan <= $chan(0)) {
    var %chans = %chans $chan(%chan)
  }
  msg $chan I am on %chans $+ , $nick $+ .
}

Don't make the mistake of forgetting to increase the variable in your while loop. If you don't make it so the while loop can be false, it will go on forever and lock up mIRC until you hit Ctrl+Break to stop it.

Goto Loops edit

Goto is a command that isn't used very often, but it can be useful, and can act as either an if statement or a while loop.

/goto point

...

:point
commands

This is basically how /goto works. You set a :point on a line somewhere in the script, and then somewhere else in the script, you use goto point to skip down to the line after the point. For example:

On *:Text:!number*:#:{
  if ($2 == 1) { goto 1 }
  if ($2 == 2) { goto 2 }
  if ($2 == 3) { goto 3 }
  else { goto huh }
  :1
  msg $chan You picked 1!
  return
  :2
  msg $chan You picked 2!
  return
  :3
  msg $chan You picked 3!
  return
  :huh
  msg $chan What number is that $+($nick,?)
}

In this example, the script will determine what number you picked and tell you what it was. If you picked something that isn't 1, 2, or, 3, the script will tell you. Notice what goto does in this script. Depending on what you picked, it will jump to a certain spot in the script.

alias who_ops {
  var %op = 1  
  :start
  if ($nick($chan,%op) isop $chan) { echo -a $v1 }
  inc %op
  if (%op <= $nick($chan,0)) {
    goto start
  }
}

This time, the /goto statement doesn't control the branching of the script, but instead causes it to loop until a certain condition is met, It's really up to you whether to use a goto or an if or while in your scripts. It all depends on what purpose you are trying to achieve.

Special Loop Commands edit

There are four special commands used in loops; /halt, /break, /continue, and /return. /break and /continue are specific to loops, but /halt and /return have special uses that involve stopping currently executing scripts or routines.

/halt edit