From WikiChip
Difference between revisions of "mirc/conditional statements"
< mirc

(made small changes only.)
m (Reverted edits by 70.69.4.61 (talk) to last revision by Sophist)
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 (number zero).  A condition has a true [[truth value]] if it is anything NOT {{mIRC|$null}}, [[$true - identifier - mIRC|$true]], or 1 (number one).
+
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.
  
 
== 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 or more 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 thing 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 room #mIRC?
+
:* Am I on #mIRC?
:* Is the %counter variable set or not?
+
:* Is the %counter variable assigned or not?
  
 
== If Statements ==
 
== If Statements ==
  
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.
+
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.
  
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.
+
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.
  
 
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 <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.
+
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.
  
 
== 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 55: Line 53:
 
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 the identifier <code>/return</code> the value to the calling routine (<code>//echo</code>); effectively preventing some of the code 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 it return the value to the calling routine, effectively preventing the other statement 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 previous if statement -- immediately used one command behind.  
+
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 basic syntax is:
 
The basic syntax is:
Line 89: Line 87:
 
== 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 <code>/elseif</code> or possibly an ending <code>/else</code>.  
+
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.  
  
 
The basic syntax is:
 
The basic syntax is:
Line 98: Line 96:
 
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 105: Line 101:
 
}</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 in a remotes script file:
+
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:
  
 
<syntaxhighlight lang="mIRC">Alias greet {
 
<syntaxhighlight lang="mIRC">Alias greet {
Line 162: Line 158:
  
 
<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 172: Line 168:
  
 
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*:#:{
+
<syntaxhighlight lang="mIRC">on $*:text:/^([!.])example$/Si:#:{
   var %send = $iif(($mid($1, 1, 1) == .), notice $nick, msg $chan)
+
   var %send = $iif($regml(1) == ., notice $nick, msg $chan)
   %send This is an example, maybe channel maybe notice
+
   %send This is an example
   %send This is another line! without the yikyak
+
   %send This is another line!
 
}</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.
 

Revision as of 07:01, 8 February 2020

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.

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 thing 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 #mIRC?
  • Is the %counter variable assigned or not?

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.

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.

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 answer 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 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 it return the value to the calling routine, effectively preventing the other statement 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 if statement.

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 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
}
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:

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$/Si:#:{
  var %send = $iif($regml(1) == ., notice $nick, msg $chan)
  %send This is an example
  %send This is another line!
}