MIRC Scripting/BeyondBasics/Loops

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
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.

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 <tt>$1</tt> equals <tt>!2</tt>. If it does, it will message the channel. Otherwise, it will move to the last resort, the <tt>else</tt> statement. This will message the channel the number was not 1 or 2.

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 <tt>/return</tt> 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
If statements can be used on the command line when double prefixed (<tt>//</tt>).

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.

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

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

$iif
<tt>$iif</tt> is the identifier version of an <tt>if</tt> command. The syntax of the identifier is:

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

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 <tt>$true</tt> to your screen. Otherwise, the command will echo <tt>$false</tt> to your screen.

While Loops
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.

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.

The function of this loop is to let someone type <tt>!channels</tt> 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 <tt>%chan</tt> is less than or equal to the number of channels you are on (<tt>$chan(0)</tt>) it will add the channel name to the variable <tt>%chans</tt> and then increase <tt>%chan</tt>. Once <tt>%chan</tt> is greater than <tt>$chan(0)</tt> it will stop and proceed with the next command.

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 <tt>Ctrl+Break</tt> to stop it.

Goto Loops
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.

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

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.

This time, the <tt>/goto</tt> 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 <tt>goto</tt> or an <tt>if</tt> or <tt>while</tt> in your scripts. It all depends on what purpose you are trying to achieve.

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