From WikiChip
Editing mirc/conditional statements

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.

Latest revision Your text
Line 1: Line 1:
 
{{mirc title|Conditional Statements}}
 
{{mirc title|Conditional Statements}}
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 {{mIRC|$null}}, [[$false - identifier - mIRC|$false]], or 0.
+
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 {{mIRC|$null}}, [[$false - identifier - mIRC|$false]], or 0 (number zero).  A condition has a true [[truth value]] if it is anything NOT {{mIRC|$null}}, [[$true - identifier - mIRC|$true]], or 1 (number one).
  
 
== Intro ==
 
== 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 things and decide what to do upon the result of that comparison. For example:
+
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?
 
:* Did the user select blue or red?
 
:* Is the number less than 10?
 
:* Is the number less than 10?
:* Am I on channel #mIRC?
+
:* Am I on room #mIRC?
:* Is the %counter variable assigned or not?
+
:* Is the %counter variable set or not?
  
 
== If Statements ==
 
== If Statements ==
  
In many occasions, you will find the need to do a different task based on a condition. The if statement allows you to control what part of your program gets executed or ignored based on conditional statement. Without a conditional statement like an if statement a script would run almost the exact same way each time.
+
In many occasions, you will find the need to do a different (changing) task based on supported not changing condition commands. The <code>if statement</code> 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 <code>operators</code> such as <code>equals; [==]</code> and <code>is-wild-match; [iswm]</code> and a modifier of <code>Not; [not equals: !=]</code>.  The entire list of control flow operators are listed, keep reading further. Without a conditional statement like an <code>if statement</code> a script would run almost the exact same way each time and you would not know for example if your <code>alias</code> is called as an <code>identifier</code> or an <code>/command</code> and you need to <code>/if</code> check whether or not you are connected to a server, also verifying <code>/set %variables</code> 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 a zero or a $null value. A false statement is one that evaluates to a zero or a $null value.
+
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:
 
The basic syntax for an if statement is:
Line 26: Line 26:
 
  2 > 3
 
  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 answer is false, 2 is not greater than 3.
+
In the example above, ">" is the symbol for the greater-than <code>operator</code>. The example above has 2 <code>operands: 2 and 3</code>. In the case of the example above, the return value is $false; 2 is not greater than 3.
  
 
== Operators ==
 
== Operators ==
Line 44: Line 44:
  
 
<syntaxhighlight lang="mIRC">Alias percent {
 
<syntaxhighlight lang="mIRC">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?
 
   ;is the percent negative?
 
   if ($1 < 0) {
 
   if ($1 < 0) {
Line 53: Line 55:
 
sample output:
 
sample output:
  
<pre>//echo -a $percent(-.33)
+
<pre>//echo -a : $percent(-.33)
(33%)
+
: (33%)
//echo -a $percent(.94)
+
//echo -a : $percent(.94)
94%</pre>
+
: 94%</pre>
  
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 it return the value to the calling routine, effectively preventing the other statement from ever getting executed.
+
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 <code>/return</code> the value to the calling routine (<code>//echo</code>); effectively preventing some of the code from ever getting executed.
  
 
== Else ==
 
== 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 if statement.  
+
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:
 
The basic syntax is:
Line 87: Line 89:
 
== ElseIf ==
 
== 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 else.  
+
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 <code>/elseif</code> or possibly an ending <code>/else</code>.  
  
 
The basic syntax is:
 
The basic syntax is:
Line 96: Line 98:
 
elseif (condition) {
 
elseif (condition) {
 
   ;do something if the /if was false, but the /elseif was true
 
   ;do something if the /if was false, but the /elseif was true
 +
}
 +
elseif (!condition) {
 
}
 
}
 
else {
 
else {
Line 101: Line 105:
 
}</syntaxhighlight>
 
}</syntaxhighlight>
  
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:
+
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:
  
 
<syntaxhighlight lang="mIRC">Alias greet {
 
<syntaxhighlight lang="mIRC">Alias greet {
Line 158: Line 162:
  
 
<syntaxhighlight lang="mIRC">Alias Example {
 
<syntaxhighlight lang="mIRC">Alias Example {
   echo -a $iif($calc(1 + 1) == 2,1+1 Equals 2)
+
   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)
+
   echo -a $iif(2 == 5, I Guess 2 Does Equals 5 After All, Nope 2 != 5)
 
}</syntaxhighlight>
 
}</syntaxhighlight>
  
Line 168: Line 172:
  
 
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.
 
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.
<syntaxhighlight lang="mIRC">on $*:text:/^([!.])example$/Si:#:{
+
<syntaxhighlight lang="mIRC">on *:text:?example*:#:{
   var %send = $iif($regml(1) == ., notice $nick, msg $chan)
+
   var %send = $iif(($mid($1, 1, 1) == .), notice $nick, msg $chan)
   %send This is an example
+
   %send This is an example, maybe channel maybe notice
   %send This is another line!
+
   %send This is another line! without the yikyak
 
}</syntaxhighlight>
 
}</syntaxhighlight>
  
 
[[Category:mIRC|conditional statements]]
 
[[Category:mIRC|conditional statements]]
 +
You can create more complex conditions using characters && AND and || OR. Example:
 +
<syntaxhighlight lang="mIRC">/if (%operlist == $null) || ((b == c) && (a == a)) {
 +
  echo -a : variable operlist is not $!null
 +
}</syntaxhighlight>
 +
 +
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:
 +
 +
<pre>
 +
alias unsecure {
 +
  if $1- == hello world { echo hello world }
 +
}
 +
</pre>
 +
<pre>
 +
alias unsecure2 {
 +
  if (hello world == $1-) echo hello world
 +
}
 +
</pre>
 +
 +
Someone could use <code>/unsecure2 hello world) /quite (unsecure quitmsg) | $chr(32) </code> and the injected code might be evaluated by some older versions of mIRC. Always use round brackets () and always use curly braces {} everywhere possible.

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)