mIRC Scripting Tutorial — File Browser

mIRCScriptingTutorialv1.0 / mIRCScriptingTutorialv1.0 / tutorial01 / statements.htm

statements.htm — 20.33 KB — Download this file

<html>
<head>
<title>SpyderWares</title>
<meta name="keywords" content="mIRC, mIRC scripts, mIRC addons, mIRC add-ons, scripts, scripting, mIRC scripting, IRC, mIRC, mIRC bots, bots">
<meta name="description" content="SpyderWares: Kewl add-ons for mIRC">
  <style>
body {
scrollbar-arrow-color: #2A2B2C; 
scrollbar-base-color: #363738;
scrollbar-face-color: #686A6C; 
scrollbar-highlight-color: #2A2B2C; 
scrollbar-shadow-color: #363738
}
.head { color: #3589FB; font-size:11; font-weight:bold; font-family:arial; position:absolute ; top:20; left:320 }
.body { color: #909090; font-size:11; font-family:arial }
.code { font-family: "courier new"; color: #008000; font-size: 8pt; }
.type { font-family: terminal; color: #C0C0C0; font-size: 8pt; }
.links { color: #3589FB; font-size:11; font-weight:bold }
  </style>
</head>
<body bgcolor="#000000" leftmargin="60" rightmargin="60" link="#3589FB" vlink="#3589FB">
  <span class=head>
    Scripting Tutorial: Statements
  </span>
  <div class=body>
    <br>
    <br>
    <br>
mIRC has a number of common statements found in other programming languages. The use and the logic is the same. They are scattered all over in the mIRC Help File so I have composed them here in a collection so that you won't need to search for them in the Help File and maybe even miss some of them. But I still suggest you to refer the Help File along with my tutorial. You can return to this portion of the page from anywhere down below by hitting the "Home" key on your keyboard.
<br><br>
<a href=statements.htm#expression>Expression Statements</a><br>
<a href=statements.htm#compound>Compound Statements</a><br>
<a href=statements.htm#if>If Statements</a><br>
<a href=statements.htm#elseif>Else If Statements</a><br>
<a href=statements.htm#goto>Goto Statements</a><br>
<a href=statements.htm#while>While Statements</a><br>
<a href=statements.htm#var>Var Statements</a><br>
<a href=statements.htm#break>Break and Continue Statements</a><br>
<a href=statements.htm#halt>Halt Statements</a><br>
<a href=statements.htm#return>Return Statements</a><br>
<p>
<b><span id="expression">Expression Statements</span></b>
<br>
Expression statements are the simplest kind of statements in mIRC. They execute only one command without any evaluation or other complex processings. Examples of expression statements are given below.
<br><br>
<span class=code>
alias d run $mircdir<br>
echo -a Hello World!<br>
set %i 10<br>
dialog -x test 
</span>
<p>
<b><span id="compound">Compound Statements</span></b>
<br>
Compound statements are a collection of commands which together define a custom identifier or perform multiple tasks together for an event. They can be piped but are best when contained within curly brackets. Piping is discouraged. Example:
<br><br>
<span class=code>
{<br>
&nbsp; &nbsp; var %a $calc($1 * 5)<br>
&nbsp; &nbsp; var %b $calc ($2 + 32)<br>
&nbsp; &nbsp; echo -a $calc(%a + %b)<br>
}
</span>
<p>
<b><span id="if">If Statements</span></b>
<br>
If statements are used to evaluate a condition and proceed next accordingly. It is used to make decisions in a program. This statement has two forms. The first is:
<br>
<br>
<span class=code>
&nbsp; &nbsp; if (condition) {<br>
&nbsp; &nbsp; command<br>
}
</span>
<br><br>
In this form if the condition is true the command is executed else it's not. For example:
<br><br>
<span class=code>
<pre>
on *:text:*:?: { 
  if ($nick isop #channel) { 
    echo -a 14This guys is an OP on #channel 
  }
}</pre>
</span>
In the above example if the nick which PMed you is an OP on the chanel called #channel, mIRC will echo "This guy is an OP on #channel" in gray (color code 14 in mIRC). If the nick is not an OP on #channel it will do nothing. Now what if you wanted mIRC to echo "This guy is just a regular chatter on #channel" if the nick is not an OP? That's where the second form comes into play - it includes an <span class=code>else</span>. The second form is:
<br><br>
<span class=code>
<pre>
on *:text:*:?: { 
  if (condition) {
    command1 
  }
  else { 
    command2 
  }
} </pre>
</span>
We rewrite our code this time using the second form to get the desired result - if the nick is not an OP on #channel mIRC will echo "This guy is just a regular chatter on #channel".
<br><br>
<span class=code>
<pre>
on *:text:*:?: { 
  if ($nick isop #channel) {
    echo -a 14This guys is an OP on #channel 
  }
  else { 
    echo -a 14This guy is just a regular chatter on #channel 
  } 
}</pre>
</span>
Now that script is certainly not a very useful one. It's only going to annoy you. So you better not use them practically. But it's good enough to test the script and understand the if-else statement.
<br><br>
Before I forget I want to remind you, by condition I don't mean strictly only one condition like <span class=code>if ($nick isop #channel) { command }</span>. You can evaluate two conditions under a condition using the logical AND or logical OR operators. An example: <br><br>
<span class=code>if (($nick isop #channel) && ($me isvoice #channel)) { <br>
&nbsp; &nbsp; echo -a $nick is an OP and I am voiced on #channel! <br>
&nbsp; } <br>
&nbsp; else { <br>
&nbsp; &nbsp; echo -a One or both of the conditions are false! <br>
&nbsp; } <br>
}
</span>
<br><br>
An you can also have many independent <span class=code>if</span> statements for an event or an alias. Here's an example:
<br><br>
<span class=code>
on *:text:*:?: { <br>
&nbsp; &nbsp; if ($nick isop #channel) echo -a This guy is an OP on #chanel. <br>
&nbsp; &nbsp; if ($me isvoice #channel) echo -a I am voiced on #channel. <br>
&nbsp; &nbsp; if ($day == Sunday) echo -a Today is the day. <br>
&nbsp; &nbsp; else echo -a Today is not Sunday. <br>
&nbsp; &nbsp; if ($me isop #channel) echo -a I am an OP on #channel. <br>
} 
</span>

<p>
<b><span id="elseif">Else If Statements</span></b>
<br>
Now what if you need to evaluate a nested if-else statement? We use the <span class=code>Else If</span> statement. It's actually a series of <span class=code>if</span> statements in which each <span class=code>if</span> is part of the <span class=code>else</span> clause of the previous statement. The format is:
<br><br>
<span class=code>
if (condition1) {<br>
command1 <br>
} <br>
else if (condition2) { <br>
command2 <br>
} <br>
else if (condition3) { <br>
command3 <br>
} <br>
else command4 <br>
}
</span>
<br><br>
If condition1 is true the script executes command1 and halts, else it checks condition2. If condition2 true it executes command2 and halts, else it checks condition3. If condition3 is true it executes command3 and halts, else (if all conditions fail) it executes command4. The following example is a working example of the Else If statement.
<br><br>
<span class=code>
<pre>
on *:text:*:?: { 
  if ($nick isop #channel) { 
    echo -a This guy is an OP on #chanel. 
  } 
  else if ($me isvoice #channel) { 
    echo -a I am voiced on #channel. 
  } 
  else if ($day == Sunday) { 
    echo -a Today is the day! 
  } 
  else { 
    echo -a None of the conditions are true! 
  } 
}</pre>
</span>
You can get the same result using the regular <span class=code>If-Else</span> statement. But in a situation like this it's preferable and more legible to use <span class=code>Else If</span> than, writing the statements out in their syntactically equivalent fully nested form:
<br><br>
<span class=code>
<pre>
on *:text:*:?: { 
  if ($nick isop #channel) { 
    echo -a This guy is an OP on #chanel. 
  } 
  else { 
    if ($me isvoice #channel) { 
      echo -a I am voiced on #channel. 
    } 
    else { 
      if ($day == Sunday) { 
        echo -a Today is the day! 
      } 
      else { 
        echo -a None of the conditions are true! 
      } 
    } 
  } 
}</pre>
</span>
<p>
<b><span id="goto">Goto Statement</span></b>
<br>
The <span class=code>goto</span> statement allows execution to jump to an arbitrary labeled point in the script, just as in Perl and C. The labelling is a little different though - the colon prefixes the label. The following example should explain you how the <span class=code>goto</span> statement works.
<br><br>
<span class=code>
alias gototest { <br>
&nbsp; &nbsp;   if ($1 == 1) goto one<br>
&nbsp; &nbsp;   else if ($1 == 2) goto two<br>
&nbsp; &nbsp;   else goto unknown<br><br>

&nbsp; &nbsp;   :one<br> 
&nbsp; &nbsp;   echo -a It's one!<br>
&nbsp; &nbsp;   halt<br><br>

&nbsp; &nbsp;   :two<br>
&nbsp; &nbsp;   echo -a It's two!<br>
&nbsp; &nbsp;   echo -a Spelled T-W-O.<br>
&nbsp; &nbsp;   halt<br><br>

&nbsp; &nbsp;   :unknown<br>
&nbsp; &nbsp;   echo -a Unkown number!<br>
&nbsp; &nbsp;   halt<br><br>

}
</span>
<br><br>
Don't forget to use the <span class=code>halt</span> at the end of the command(s) under a label or else the next label will be executed too. Applying it's ability to jump anywhere in the code the <span class=code>goto</span> statement can be used to loop too. The example below is one such. <br><br>
<span class=code>
alias test { <br>
&nbsp; &nbsp;   var %i 0 <br>
&nbsp; &nbsp;  :again <br>
&nbsp; &nbsp;  if (%i <= 10) { <br>
&nbsp; &nbsp; &nbsp;    echo -a %i <br>
&nbsp; &nbsp; &nbsp;    inc %i <br>
&nbsp; &nbsp; &nbsp;    goto again <br>
&nbsp; &nbsp;  } <br>
&nbsp; else echo -a Counted from 0 to 10. <br>
} 
</span>
<br><br>
You could get into an infinite loop if you are not cautious. The mIRC Help File informs "Using a goto incorrectly could lead to an infinite loop. You can break out of a currently running script by pressing Control+Break." If you really need to loop use the <span class=code>While</span> statement instead it's much better. And it's the 'official' loop statement.
<p>
<b><span id="while">While Statement</span></b>
<br>
Just as the <span class=code>If</span> statement is the basic control statement that allows the script to make decisions, the <span class=code>While</span> statement is the basic statement that allows the script to perform repetitive actions. It has the following syntax:
<br><br>
<span class=code>
while (condition) { <br>
&nbsp; &nbsp; command <br>
}
</span>
<br><br>
The <span class=code>While</span> statement works by first evaluating the (condition), if it's false it moves on to the next statement in the script else the statement that forms the body of the loop is executed and the (condition) is evaluated again. If it's false the execution goes to the next statement in the script else the body of the loop is executed and the (condition) is evaluated again. It goes on and on this way till the (condition) returns false. The concept will become clear with the following example:
<br><br>
<span class=code>
alias whileloop { <br>
&nbsp; &nbsp; var %i 0 <br>
&nbsp; &nbsp; while (%i <= 10) { <br>
&nbsp; &nbsp; &nbsp; echo -a %i <br>
&nbsp; &nbsp; &nbsp; inc %i <br>
&nbsp; &nbsp; } <br>
&nbsp; echo -a Counted from 0 to 10. <br>
}
</span>
<br><br>
We create an alias named "whileloop". When envoked it first creates a local variable "%i" with the value 0. Now comes the <span class=code>condition</span> part. The condition is <span class=code>while (%i <= 10)</span>. Since the value of %i is 0, the condition return true and the body of the loop is executed. It echoes the current value of %i that's 0 and then increases the value of %i by one unit - to 1. The new value of %i (1) is evaluated again to see if it's less than or equal to 10. It returns true, the body of the loop is executed again and the new value of %i is evaluated again. It goes on this way till the value of %i becomes 11 which is more than 10. The condition returns false and the execution moves on to the next statement and echoes "Counted from 0 to 10." 
<br><br>
Compare the loop examples for the <span class=code>Goto</span> and <span class=code>While</span> statements. They return the same results. Which one do you prefer to use?
<p>
<b><span id="var">Var Statements</span></b>
<br>
Var statements are covered in the chapter on <a href=variables.htm#var>Variables</a>.
<p>
<b><span id="break">Break and Continue Statements</span></b>
<br>
The <span class=code>Break</span> statement is used to break out of a loop after evaluating a condition under the main condition (the condition which started the loop). Here's an example to demonstrate it's possible use. <br><br>
<span class=code>
<pre>
alias search { 
  var %i 1 
  var %n $nick($2,0) 
  while (%i <= %n) { 
    var %nick $nick($2, [ %i ] ) 
    echo -a %nick 
    if (%nick == $1) { 
      echo -a -> %nick found on $2 $+ ! 
      break 
    } 
    inc %i 
  } 
  echo -a Search Finished. 
} </pre>
</span>
This alias will search for a nick on a channel. The format is <span class=code>/search &lt;nick> &lt;channel></span>. For it to work you will need to be on that channel of course. Not just to explain the <span class=code>break</span> statement, this script is also a good loop example and also shows the use of an important identifier in mIRC <span class=code>$nick()</span>.
<br><br>
The above script is explained here: First a local variable <span class=code>%i</span> with the value of 1 is created. Next another local variable <span class=code>%n</span> is created; it's value is the value returned by the <span class=code>$nick()</span> identifier. We use <span class=code>$nick($2,0)</span> where $2 is the channel name (the second parameter); it returns the number of nicks on the channel.
<br><br>
The <span class=code>while (%i <= %n)</span> condition defines that as long as %i is less than or equal to the number of nicks on the channel execute the body of the loop. The body of the loop: Set a local variable <span class=code>%nick</span> with the value returned by the <span class=code>$nick($1, [ %i ] )</span> identifier. Since initially the value of %i is 1 the value of the %nick variable is the first nick on the channel. The value of %nick is echoed next. Then comes an <span class=code>If</span> statement which checks for the condition: if the value %nick is equal to the first parameter you supplied to the identifier (the nick you are looking for). If it returns true, the script echoes "&lt;nick> found on &lt;channel>!" and proceeds to the next statement and echoes "Search Complete.". If it returns false the value of %i is increased and the same process continues. It goes on till all the nicks on the channel are checked and finally echoes "Search Complete.".
<br><br>
It's not a very practical script, it was just to demonstrate how the <span class=code>break</span> statement works.
<br><br>
The <span class=code>Continue</span> statement is used go back to the beginning of the loop after evaluating a condition within the body of a loop. An example using the <span class=code>continue</span> statement is shown below. It prints all the numbers from 0 to 10 except 5.
<br><br>
<span class=code>
<pre>
alias no5 { 
  var %i 0
  while (%i <= 10) {
    if (%i == 5) {
      inc %i
      continue
    }
    echo -a The number is: %i
    inc %i
  }
  echo -a -
  echo -a See I didn't echo number 5! 
}</pre>
</span>
<p>
<b><span id="halt">Halt Statements</span></b>
<br>
The <span class=code>Halt</span> statement is used to halt the further processing of a script. It can be used to prevent default responses by mIRC to various event too. Now in the example for the <span class=code>break</span> statement you might have wished the script echoed "&lt;nick> found on &lt;channel> and stopped completely if the nick is on the channel else echo "&lt;nick> not found on &lt;channel>." We can do it with the use of the <span class=code>halt</span> statement.
<br><br>
<span class=code>
<pre>
alias search { 
  var %i 1 
  var %n $nick($2,0) 
  while (%i <= %n) { 
    var %nick $nick($2, [ %i ] ) 
    echo -a %nick 
    if (%nick == $1) { 
      echo -a -> %nick found on $2 $+ ! 
      halt 
    } 
    inc %i 
  } 
  echo -a Search Finished: $1 not found on $2 $+ .
}</pre>
</span>
Wondering how you halt the default responses of mIRC to vaious events? You suffix the asterix (*) with a caret (^), specify your command and then use the <span class=code>haltdef</span> command. Here's an example: 
<br><br>
<span class=code>
on *^:join:#: { <br>
&nbsp; echo -a Hey! $nick has joined $chan $+ . <br>
&nbsp; haltdef <br>
}
</span>
<br><br>
Not just for the ON JOIN event, it can be used for all other mIRC events including CTCP. The only exception is the CTCP VERSION event. You can include your own response to that event but <span class=code>haltdef</span> won't halt mIRC from sending the info about your mIRC version and that Khaled wrote the software.

<p>
<b><span id="return">Return Statements</span></b>
<br>
This statement halts the currently executing routine and allows the calling routine to continue processing. If the routine was not called by any routine the whole script is halted. The <span class=code>return</span> statement is also used to return values from an Alias. That's how we create <a href=identifiers.htm#custom>Custom Identifiers</a>. <a href=statements.htm#routine>What is a routine</a>?.
<br><br>
If you are wondering "What exactly is the difference between <span class=code>halt</span> and <span class=code>return</span>?", read on. When you use <span class=code>halt</span>, the processing of the script is stopped then and there. When you use <span class=code>return</span>, the processing of the routine (alias/custom identifier) stops and it returns a value to the calling routine (mIRC command/alias/custom identifier). The return value can be null or non-null. And the calling routine can continue executing. The examples below demonstrate the similarity and difference between <span class=code>halt</span> and <span class=code>return</span>.
<p>
<span class=code>
<pre>
alias hnr {
  echo -a The next line will halt the script with the halt command.
  halt
  echo -a This line won't be echoed.
}
</pre>
</span>
Replace <span class=code>halt</span> with <span class=code>return</span> in the above alias, the result is the same. It may seem like <span class=code>halt</span> and <span class=code>return</span> do the same thing. In an isolated alias actually it is so; but when you call an <a href=aliases.htm>Alias</a> or a <a href=identifiers.htm#custom>Custom Identifier</a> from another alias or a custom identifier using <span class=code>halt</span> instead of <span class=code>return</span> and vice versa makes a difference of day and night.
<br><br>
<span class=code>
<pre>
alias hnr {
  echo -a Calling an alias.
  myAlias
  echo -a Only return could have allowed this!
}

alias myAlias {
  echo -a This is from myAlias!
  halt
}
</pre></span>
Replace <span class=code>halt</span> with <span class=code>return</span> in the alias <span class=code>myAlias</span> and execute <span class=code>/hnr</span> to see the difference it makes. 
<br><br>
<span id=routine>A routine</span> is a chunk of code, usually an alias or a custom identifier. It's the same as module or function or routine in other programming languages. Since Khaled mentioned the term routine in the Help File, we will use routine for mIRC functions or modules. And mIRC routines can either be multi-lined Aliases or Custom Identifiers.


<p>
Here's an interesting little alias I wrote: it checks if a number is prime or not. Study the code till you understand, it will be 'enlightening'. The way to execute the alias is <span class=code>/prime &lt;number></span>. By the way, 31337 is a prime number =:D <br><br>
<span class=code>
<pre>
alias prime {
  var %i 2
  while (%i < $calc($1-1)) {
    var %n $calc($1 % %i)
    if (%n == 0) {
      echo -a $1 is not a prime number!
      halt
    }
    inc %i
  }
  echo -a $1 is a prime number!
} 
</pre>
</span>

<p>
Now that you have learnt about Aliases, Identifiers, Variables, Loops, Conditions, Operators and all, you are ready to move on to the next step -  Remotes.

<p>
       
      <br>
      <a href=operators.htm>
        Back</a>
      |
      <a href=index.htm>
        Table of Contents</a>
      |
      <a href=remotes.htm>
        Remotes</a>
      
<br>
<img src=img/spacer.gif height=50 width=10>
<br>
<center>
<img src=img/spyder.gif> 
<br><br>
<font size=-2>
Copyright &copy; 2002-2004 SpyderWares.
<br>
Feel free to distribute this tutorial in part or whole, just make sure the credits stay intact.
</font>
<br>
<a href=http://spyderwares.com target="_blank">
http://spyderwares.com
</a>
</center>

<p><br><br>

</div>
</body>
</html>