From Scriptwiki
Revision as of 17:41, 12 September 2005 by Tovrleaf (talk | contribs)

Jump to: navigation, search

If-then-else statements

The If-then-else statement allows you to compare values and execute different parts of a script based on that comparison.

if (v1 operator v2) { commands }
elseif (v1 operator v2) { commands }
else { commands }

If the first statement is true, commands inside of first brackets are executed. If the first if-statement returns false, script starts looking for elseif-statement. Elseif-statement can be only accessed if none of the same groups if's have not returned true before. And at last, if none of the statements were triggered, commands in else-statement are executed.

One if structure 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 effects 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 return
B               If B is true, 
  C                 check if C, D or E returns true
  F             If none of these got triggered, return F
G               G is checked only if A or B were not triggered
H               If none of previous wasn't matches, H is returned

The Operators

==        equal to
===       equal to (case-sensitive)
!=        not equal to
<         less than
>         larger than
>=        larger than or equal to

<=        smaller than or equal to
//        v2 is a multiple of v1
\\        v2 is not a multiple of v1
&         bitwise comparison

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      nickname v1 is on channel v2
isop      nickname v1 is an op on channel v2
ishop     nickname v1 is a halfop on channel v2
isvoice   nickname v1 has a voice on channel v2
isreg     nickname 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 internal ban list

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.

To negate an operator you can prefix it with an ! exclamation mark.

Few examples

if (cat isin $1-)

matches for "a cat", catalog and any string where "cat" is in.

iswm stands for is a wildcard match. Operators:

*  0..N character
?  1 character
if (*bat* iswm $1-)

allows charter before string and after it, but there's no need for it. See Wildcard for more help.

if (ca?* iswm $1-)

requires one character after "ca" and allows 0..N character after it. So it would match for cat, cab, cabin etc.

Not all comparations need two parameters.

if ($1- isupper)

returns true, if $1- oly contain uppercase letters. So ABCD returns true, but even one lowercase letter makes it return false.

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

See Also