At some point in scripting you need to make mIRC compare one value with another. This might be to see if a word in text matches a word you want to check for. It might be to check if a certain time has been reached. It could be many things.
What it mostly is though is a way of making mIRC do a little thinking. The IF statement makes mIRC ask itself if one thing is equal to, or different from, more or less than another thing. When we use the IF statement we are teaching mIRC to ask 'is it?'.
Comparing two or more values is essential to any decision making. The human brain works out how tired or hungry or whatever we are by comparing our current state to other conditions. Our minds tell us we are hungry when it finds our current state of hunger is higher than the state we like it to be, etc.
mIRC uses the IF statement to compare values.
IF (<value1> == <value2>)
Compares value1 to value2 to determine if they are the same.
if (<value1> <operator> <value2>) { <commands> }
is the form mIRC accepts for making comparisons. The () brackets and {} brackets
shown should be used as shown. The <> brackets must not be included, they
are used in the example simply to show that this is a necessary parameter.
The () brackets are not always essential, but do help mIRC to work out which are the values you want compared. The {} brackets are, as covered in the MIRC Aliases page, able to contain multiple commands if a new line is used for each command.
<value1> is the first value you want mIRC to look at, while <value2> is the value you want mIRC to compare it to. The <operator> parameter is the kind of comparison you want mirc to make.
Sign | Meaning |
== | equal to. |
=== | equal to (including the same capital letters) |
!= | not equal to. |
< | less than (value1 is less than value2) |
> | more than (value1 is more than value2) |
<= | less than or equal to (value1 is not more than value2) |
>= | more than or equal to (value1 is not less than value2) |
// | is a multiple of (divides evenly by) |
\\ | is not a multiple of (does not divide by) |
isin | value1 is a string in value2 |
!isin | value1 is NOT a string in value2 |
ison | value1(nick) is on value2(channel) |
!ison | value1(nick) is NOT on value2(channel) |
isop | value1(nick) is an operator on value2(channel) |
There are a few more operators in mIRC's If-Then-Else statements. Read the mIRC help files to study them if you wish. The ones above are more than enough for our purposes however.
I have not listed them for you to learn them. I do not expect you to remember them all. I have detailed the operators purely for you to get an idea of the kind of comparisons mIRC can make. All you need to remember is that mIRC scripts can do many comparisons of values.
I will show some examples in a moment to demonstrate exactly what use the IF statement can be. First however we need to cover the 'Then' part.
if (<value1> <operator> <value2>) { <commands> }
The <commands> are the Then part of the statement. It is what to do if
the question we made mIRC ask about the values is true.
if (<value1> == <value2>) { <commands> }
tells mIRC that if value1 is the same as value2 it should perform the command
<commands>.
Now its time for a few examples to help ensure that the principles of the IF statement are clear.
if ($me != %favenick) { /nick %favenick }
The above makes mIRC compare your current nick($me) with a variable called
%favenick (in which we would have saved your favourite nickname). The !=
operator makes mIRC look to see if your current nick is different to your
favourite nick. If the two are different mIRC will run the command /nick
to change your nick to your favourite one(%favenick).
if ($time == 10:00:00) { /echo -a Its Ten O'clock }
This one asks mIRC to check if the current time ($time) is equal to 10:00:00
(the display for 10 o'clock in hours: minutes: seconds), and if it is, to
display only to the user (/echo) in the active window (-a) the text 'Its
Ten O'clock'.
The Else follows one or more If statements, and provides an instruction to be followed when the If statement was untrue.
IF if this comparison is true
Then do this
Else otherwise do this instead
You do not have to have an Else at the end of If statements but it is often useful to have one. The Else statement is similar to an If statement but needs no values or operators.
ELSE { <commands> }
is the format for an else statement. As always, multiple commands can be given
between the {} brackets provided a new line is used for each command.
Example:
set %answer $?="Do you like hamburgers (yes/no) ?"
if (%answer == yes) {
echo -a You do like hamburgers
set %likesburgers Yes
}
if (%answer == no) {
echo -a You don't like hamburgers
set %likesburgers No
}
else {
echo -a You typed %answer instead of yes or no
set %unsure Yes
}
Don't be alarmed by the length of the silly script above. The first line opens a dialog box for you to answer a question and stores your answer as a variable called %answer.
The first If statement looks to see if the answer given was the word 'yes'. If it was it echoes a comment to the screen and sets a variable called %likesburgers to record that you do like burgers.
The second If statement looks to see if your answer was no. If your answer was no it will echo the result to the active window, and save the variable %likesburgers with a value of No.
The Else statement looks to see if neither of the If statements were true, in which case you must have typed something other than yes or no. It echoes this information to the screen and sets a different variable called %unsure with a value of yes.
Its a silly script and not one I recommend using but it does illustrate the way multiple commands may be issued.
There is one other statement type in the If-Then-Else group, the ELSEIF statement. A second If statement will check its values regardless of whether or not the first If statement was true. An ELSEIF statement is just like an If statement but will only check the values if the preceeding statement was not true.
To check a second pair of values only if the first statement were true there are at least two methods. One is to put another If statement INSIDE the command area (between the {} brackets). The other is to reverse the question of the first statement so that you ask the exact opposite, and then use an elseif statement.
For example, to determine if a number is between 1 and 10 either of the following scripts would do the job:
script 1:
if (%number <= 10) {
if (%number >=1) {
echo -a Yes, %number is in range 1-10
}
}
else echo -a %number is not a 1-10 number
script 2:
if (%number < 1) {
echo -a %number is not a 1-10 number
}
elseif (%number > 10) {
echo -a %number is not a 1-10 number
}
else echo -a Yes, %number is in range 1-10
Those two scripts both attain the same result despite using different methods and either would suffice for the purpose.
Choose a Subject from the list to continue. (Some subjects contain several tutorials.)