If-Then-Else
- Note: This is a reference. For a tutorial please see the basic control tutorial.
The If-then-else statement allows you to compare values and execute different parts of a script based on that comparison. This is a basic structure for any script that includes anything more than one-liners!
if (v1 operator v2) { commands1 } elseif (v1 operator v2) { commands2 } else { commands3 }
If the first statement (line) is $true, commands inside the first brackets are executed. If the first if-statement returns $false, script starts looking for an elseif-statement. An elseif-statement is only triggered if the group's if-statement returned $false before. And at last, if none of the if or elseif statements were triggered, commands in else-statement are executed.
One if structure/group can consist of one main if-statement, after it there can be 0 .. N elseif-statements. There can be only one else, which can be understood as the default statement, if none one before were triggered. There doesn't need to be else-statement though.
Every if statement is handled separatly and none of others affect in another.
if (A) { ... } elseif (B) { if (C) { ... } elseif (D) { ... } elseif (E) { ... } else { F } } elseif (G) { ... } else { H }
If structure would be hierarchical represented it would look something like this
A If A is true, execute its commands and skip the rest B If B is true, \ C check if C, D or E returns true D E F If C, D and E were false, execute F G If A and B were false and G is true, execute this H If A, B and G were all false, execute H
The Operators
== equal to === equal to (case-sensitive) < less than > larger than <= less than or equal to >= larger than or equal to // v2 is a multiple of v1 \\ v2 is not a multiple of v1 & bitwise comparison ! negation operator (!= is opposite of == and !isin is "is not in") isin string v1 is in string v2 isincs string v1 is in string v2 (case sensitive) iswm wildcard string v1 matches string v2 iswmcs wildcard string v1 matches string v2 (case sensitive) isnum number v1 is a number in the range v2 which is in the form n1-n2 (v2 optional) isletter letter v1 is a letter in the list of letters in v2 (v2 optional) isalnum text contains only letters and numbers isalpha text contains only letters islower text contains only lower case letters isupper text contains only upper case letters ison if v1 is on channel v2 isop if v1 is an op on channel v2 ishop if v1 is a halfop on channel v2 isvoice if v1 has a voice on channel v2 isreg if v1 is a normal nick on channel v2 ischan if v1 is a channel which you are on. isban if v1 is a banned address in the internal ban list for channel v2 isaop if v1 is a user in your auto-op list for channel v2 (v2 optional) isavoice if v1 is a user in your auto-voice list for channel v2 (v2 optional) isignore if v1 is a user in your ignore list with the ignore switch v2 (v2 optional) isprotect if v1 is a user in your protect list for channel v2 (v2 optional) isnotify if v1 is a user in your notify list.
Note: To negate an operator you can prefix it with an exclamation mark (!).
==
if (cat == cAt) ;Returns true since == is case-insensitive. if (cat === cAt) ;Returns false since === is case-sensitive.
The above checks that v1 (cat) matches v2 (cAt).
!=
This isn't actually an operator, but a combination of two operators ! and =. And actually, = isn't an operator either, but in mIRC scripting it can be used instead of == and it works just the way == works. In most mIRC scripts you only see !=, which actually shouldn't be used. Instead, !== should be used to negate ==.
if (cat != cAt) ;Returns false since != is case-insensitive and both values v1 and v2 are equal. if (cat !== caT) ;Returns false since !== is the same as != if (cat !=== cAt) ;Returns true since !=== is case-sensitive and both values v1 and v2 are not equal when case is taken in to consideration. if (!$away) ;Returns $true if you are NOT away ($away returns $false and ! negates it to $true).
<
if (6 < 9) ;Returns true because the numerical value 6 is less than 9.
Checks that v1 (6) has a lower numerical value than v2 (9)
>
if (6 > 9) ;Returns false because the numerical value 6 is not greater than 9.
Checks that v1 (6) has a higher numerical value than v2 (9)
<=
if (6 <= 9) ;Returns true because the numerical value 6 is less than or equal to 9.
Checks that v1 (6) has a lower or equal numerical value than v2 (9).
>=
if (6 >= 9) ;Returns false because the numerical value 6 is not greater than or equal to 9.
Checks that v1 (6) has a greater or equal numerical value than v2 (9).
//
if (3 // 9) ;Returns true because 3 is a multiple of 9.
Multiples of 9 are numbers that when devided in to 9 result in an integer. You are not limited to positive numbers.
\\
if (3 \\ 9) ;Returns false because 3 is a multiple of 9.
\\ is the negate of //
&
if (N1 & N2) ; This will compare the bits that are turned on in the decimal N1 AND N2 in the same way $and() would.
As long as N1 and N2 share atleast 1 bit the condition is returned as true.
if (13 & 11) ;Is true, see the table below for explanation. echo -ag $and(13,11) ;returns 9.
If you break down 13 and 11 into their bits as below:
Binary | Value |
1101 | 13 |
1011 | 11 |
1001 | 9 |
if (11 & 4) ;Is false. echo -ag $and(11,4) ;returns 0.
Binary | Value |
1011 | 11 |
0100 | 4 |
0000 | 0 |
Above you can see, 13 and 11 both have the first bit AND fourth bit on, so the statement is true. The binary 1001 value has a decimal value of 9. Also you can see, 11 and 4 do not share any common bits, so the statement is false.
isin
if (cat isin $1-)
Matches for "a cat", "catalog", "implicate " and any string where "cat" is in.
iswm
iswm stands for is wildcard match.
Operators:
* 0 or more characters ? 1 character & 1 word (atleast 1 or more non-space characters)
if (home*away*lost iswm $1-)
This would match anything starting with home and ending in lost, with away being between home and lost. It would match, "homeawaylost", "home away lost", "home is missed when you are away or lost", etc.
if (ca?* iswm $1-)
Requires one character after "ca" and allows 0 or more character after it. So it would match for cat, cab, cabin etc.
if ($(I & you) iswm $1-)
Matches any string that consists of "I + one word + you". Examples would be, "I love you", "I hate you", "I moo you", it would not however match, "I don't love you" since "don't love" contains a space. Note the $( ), it is needed to prevent mirc treat this statement as bitwise comparison.
isnum
if ($1 isnum) ;Returns true if $1 is of numeric value if ($1 isnum 10-) ;Returns true if $1 is number higher than or equal to 10 if ($1 isnum 20-30) ;Returns true if $1 is a number between 20 and 30 inclusive
The examples above checks $1, to see;
Is it a number?
Is it a number above 10?
Is it a number between 20 and 30?
isletter
if ($1 isletter) ;Returns true if $1 is a letter, any letter if ($1 isletter abcdefg) ;Returns true if $1 is in the string of letters abcdefg if ($1 isletter HelloWorld) ;Returns true if $1 is in the string of letters HeloWrd
- Note that the checking is case-sensitive, the letter h will not return true if checked against the string HelloWorld.
isalnum
if (Dana34 isalnum) ;returns true as Dana34 contains letters and numbers only if (Dana_ isalnum) ;returns false as _ is neither letter nor number
Matches whether the string consists of letters and numbers only.
isalpha
if (Dana isalpha) ; returns true as Dana consists of letters only if (foo42 isalpha) ; returns false as 42 is no letter
Isalpha checks whether the word consists of letters only. It doesn't matter whether there are upper or lower case.
islower
if (moo islower) ; returns true as all letters in moo are lower case. if (m0o islower) ; returns true as well as all letters in m0o are lower case. if (mooO islower) ; retursn false as there is an upper O in mooO.
Note that islower checks whether all letters in the string are lower case. There can still be numbers or other characters in the string $1.
isupper
if (MOO isupper) ; returns true as MOO consists of upper case letters only if (1234 isupper) ; returns true as all letters in 1234 are upper case, as there are none.
Note that it checks whether all letters in $1 are upper case. There can still be numbers or other characters in $1.
ison
if (Dana ison #help.script) ; returns true as Dana is in the channel #help.script if (foo !ison #help.script) ; returns true as foo is not in #help.script
This checks whether $1 is in the channel $2. Note that you need to be in the same channel to use this.
isop
if (Dana isop #help.script) ; returns true as Dana is an operator in #help.script if (foo isop #help.script) ; returns false as foo is no operator in #help.script
Checks whether $1 is an operator in $2. Note that you need to be in the same channel to use this.
ishop
if (moo ishop #mIRC) ; returns true if moo is an half operator in #mIRC
Note that QuakeNet does not support halfops. Note also that you need to be in the same channel to use this.
isvoice
if (Dana isvoice #help.script) ; returns true as Dana has voice in #help.script. if (foobar isvoice #help.script) ; returns false as foobar has no voice in #help.script.
This checks if $1 has voice in $2 or not. Note that you need to be in the same channel to use this.
isreg
if (Dana isreg #help.script) ; returns false as Dana is no regular user in #help.script. if (foobar isreg #help.script) ; returns true as foobar is a regular user in #help.script.
This checks whether $1 is a regular user (no voice, no half operator, no operator) in $2. Note that you need to be in the same channel to use this.
ischan
if (#help.script ischan) ; returns true as you are in #help.script. if (#foobar ischan) ; returns false as you are no in #foobar.
Note that it returns true only if you are in this channel.
isban
if (idiot!*@* isban #mychan) ; returns $true if idiot!*@* is in your Internal Ban List for the channel #mychan. if ($ial($me) isban $chan) ; returns $true if you are banned on the current channel.
Note that isban performs two different checks, depending on whether v1 contains wildcards or not:
- if v1 contains wildcards, isban will return $true if v1 is an exact ban in the IBL;
- if v1 does not contain wildcards, isban will return $true if one or more bans match that address.
isaop
if (foobar isaop) ; returns true if foobar is in your autoop list for any channel. if (foobar isaop #help.script) ; returns true if foobar is in your autoop list for #help.script.
To put someone in your autoop-list, take a look at /aop.
isavoice
if (foobar isavoice) ; returns true if foobar is in your autovoicelist for any channel. if (foobar isavoice #help.script) ; returns true if foobar is your autovoicelist for #help.script.
To put someone in your autovoicelist, take a look at /avoice.
isignore
if (foobar isignore) ; returns true if foobar is in your ignore list. if (foobar isignore c) ; returns true if foobar is in your ignore list with switch -c.
To get a more in detail explanation of these switches, take a look at /ignore.
isprotect
if (foobar isprotect) ; returns true if foobar is in your protect list. if (foobar isprotect #help.script) ; returns true if foobar is in your protect list for #help.script.
To get more information about protection, see /protect.
isnotify
if (Dana isnotify) ; returns true if Dana is in your notify list.
Take a look at /notify to add someone to your notifylist. If someone in your notifylist connects (disconnects), the On notify event (On Unotify event) is triggered.
Combining comparisons
You can combine comparisons by using the && for AND and || for OR characters.
var %c = 5 if (%c < 6) && (%c > 0) ; returns true because %c is both, smaller than 6 and greater than 0 if (%c < 6) || (%c isalpha) ; returns true because %c is lower than 6. ; note that %c is not alphapetical and returns false, but || matches for 1 .. N true values. if (%c < 6) && (%c isalpha) ; returns false if (%c < 6) && (%c !isalpha) ; ! negates the operator, this this returns true
Notes
Not all comparations need two parameters
if ($1- isupper)
Returns true if $1- contains only uppercase letters. So ABCD returns true, but even one lowercase letter makes it return false.
Use parenthesis correctly
(A) || (B) && (C) <- logical order for this is to check if either one of A or B is true, and C is true, but if you wish to check that B and C must be true, or A is true, you'll need to do (A) || ((B) && (C))
This article covers only if-then-else string operators
You can't use
if (foo isin test.txt)
To check the content of a text file, use $read to search through a file via its search switches.
See Also
- $v1 & $v2 - These identifiers allow you to retrieve the values of the last if condition.
- Basic control tutorial - An in depth tutorial on how to use these statements