- If Then Else
- Operators
- While Loops
- Goto Loops
- Groups
A conditional statement is a control flow construct in mIRC that can execute a set of commands only after comparing or testing a condition. A condition in mIRC has a false truth value if it's $null, $false, or 0 (number zero). A condition has a true truth value if it is anything NOT $null, $true, or 1 (number one).
Contents
Intro
You almost never want a script to simply do the same exact thing over and over. In many occasions, you want it to compare two or more things and decide what to do upon the result of that comparison. For example:
- Did the user select blue or red?
- Is the number less than 10?
- Am I on room #mIRC?
- Is the %counter variable set or not?
If Statements
In many occasions, you will find the need to do a different (changing) task based on supported not changing condition commands. The if statement
allows you to control what part of your program gets executed and not executed (by using indentation spacing of code text). Execution flow is based on conditional operators
such as equals; [==]
and is-wild-match; [iswm]
and a modifier of Not; [not equals: !=]
. The entire list of control flow operators are listed, keep reading further. Without a conditional statement like an if statement
a script would run almost the exact same way each time and you would not know for example if your alias
is called as an identifier
or an /command
and you need to /if
check whether or not you are connected to a server, also verifying /set %variables
these; are very common scripting concepts in mIRC program scripts.
An if statement executes a block of code only if the condition is True. So what does true mean? A true statement is one that evaluates to anything but not a zero or a $null or an $true value. A False statement is one that evaluates to a zero or $false or a $null value.
The basic syntax for an if statement is:
if (condition) { ;code to execute only if the condition was true }
A typical conditional statement consists of two operands and an operator. An operand is an entity on which an operation is performed. Take for example the following conditional statement:
2 > 3
In the example above, ">" is the symbol for the greater-than operator
. The example above has 2 operands: 2 and 3
. In the case of the example above, the return value is $false; 2 is not greater than 3.
Operators
- Main article: Operators - mIRC
mIRC provides the following types of operators:
- Arithmetic Operators
- Math Comparison Operators
- String Comparison Operators
- Logical Operators
- Channel-related Operators
- List-related Operators
example
Take a look at the following example. In this alias we take a percentage in decimal format and return it in a human readable way. We also want a negative percent to be enclosed by a pair of parentheses.
Alias percent { ;is the alias called as an $identifier if ($isid == $false) { echo -a : error'd with /percent, use example $percent(0.1) instead | return } ;is the percent negative? if ($1 < 0) { return ( $+ $calc($1 * 100) $+ % $+ ) } return $calc($1 * -100) $+ % }
sample output:
//echo -a : $percent(-.33) : (33%) //echo -a : $percent(.94) : 94%
In the example above, we used an if statement to check if the user input ($1) was less-than zero so we could handle it differently than a positive decimal number. When the if statement evaluates to true, the code inside its body, enclosed by a pair of curly brackets, get executed. In our case it also made the identifier /return
the value to the calling routine (//echo
); effectively preventing some of the code from ever getting executed.
Else
The else statement gives you the ability to execute a block of code when the conditional statement of the if part was false. Notice the else part does not have a conditional statement of its own, it simply acts upon the result of the previous if statement -- immediately used one command behind.
The basic syntax is:
if (condition) { ;code to execute only if the condition was true } else { ;code to execute only if the condition was false }
In the example below, we assign the appropriate time of day to the "%time" variable. We then displayed the result to the screen. We used an if statement to check if the time (just the hours) is less than 10. If true, set %time to day, else, set %time to morning. Depending on the time of day for you, the result will be "Good morning!" or "Good day!".
Alias greet { if ($time(H) > 10) { var %time = day } else { var %time = morning } echo -a Good %time $+ ! }
ElseIf
I am sure at this point you are already thinking what If you had more than one condition that needs to go to a different block of code. An elseif statement is when you have multiple conditional statement that each needs to do something different. If the initial if statement returned false, mIRC will then move on to the elseif and evaluate its condition just like if it was a normal if. If the condition was true, it will execute its body. If the condition of the elseif was false, it will move on to the next /elseif
or possibly an ending /else
.
The basic syntax is:
if (condition) { ;do something if the condition was true } elseif (condition) { ;do something if the /if was false, but the /elseif was true } elseif (!condition) { } else { ;do something if both the if and the elseif were false }
How would that look in a real scenario? Remember our time of day script? What if we wanted to check if its noon or night as well?. Lets write it down in a remotes script file:
Alias greet { var %hours = $time(H) if (%hours < 10) { var %time = morning } elseif (%hours == 12) { ;it's 12 o'clock var %time = noon } elseif (%hours > 20) { ;it's passed 8:00PM var %time = night } else { ;if it's 11 or 1-7PM var %time = day } echo -a Good %time $+ ! }
Reference of parameters
mIRC provides two identifiers to retrieve the first or second parameter of the conditional statement. Please note, the identifiers will return the first and second parameter of the last condition following short-circuit evaluation guidelines.
$v1 and $v2
For example:
Alias ifExample { if (4 < 5) { echo -a yes, $v1 is less than $v2 } }
Will have the following output:
yes, 4 is less than 5
IIF Identifier
IIF, inline if, is a built in identifier that evaluates a condition, similar to /if statement, and returns one of two values if the condition was true or false.
Syntax:
$iif(condition, <code for true>) ;or $iif(condition, <code for true>, <code for false>)
Note: Unlike a normal identifier, only the true or only the false part of the identifier gets evaluated depending on the condition.
iif example
Alias Example { echo -a $iif($calc(1 + 1) == 2, 1+1 Equals 2) echo -a $iif(2 == 5, I Guess 2 Does Equals 5 After All, Nope 2 != 5) }
The code above generates the following output:
1+1 Equals 2 Nope 2 != 5
A common usage for an inline if is to decide where to send a reply of a command. For example on some networks ! means a channel message while . means a notice.
on *:text:?example*:#:{ var %send = $iif(($mid($1, 1, 1) == .), notice $nick, msg $chan) %send This is an example, maybe channel maybe notice %send This is another line! without the yikyak }
You can create more complex conditions using characters && AND and || OR. Example:
/if (%operlist == $null) || ((b == c) && (a == a)) { echo -a : variable operlist is not $!null }
In the code above an extra set of round brackets are used to create only two conditions out of three; the second condition is a set of two conditions in one.
For security reasons it is always recommended to provide round brackets around conditions. But not around the whole condition which used to be valid code; but not any longer. You want to use the most brackets possible, without total coverage. So, basically you will always use round brackets but never an more brackets than whats needed to secure the code. Also you will need to use curly braces which are not required by single line code but should be used for security reasons. mIRC is more secure now than ever before and if you play your cards right you can have backward compatibility with older mIRC versions that will NOT be unsecure. Unsecure code examples:
alias unsecure { if $1- == hello world { echo hello world } }
alias unsecure2 { if (hello world == $1-) echo hello world }
Someone could use /unsecure2 hello world) /quite (unsecure quitmsg) | $chr(32)
and the injected code might be evaluated by some older versions of mIRC. Always use round brackets () and always use curly braces {} everywhere possible.