Basic control tutorial
Control statements are, very simply, statements which control which parts of your code gets executed. You should know much about aliases and events (Particularly the on text event) before reading this.
Boolean
A 'boolean' statement is a statement that can have one of two values, "true" or "false". For example:
I have a donkey.
This is a boolean statement because it is either true, or false. Control structures operate under this pretense, "If something is true, do thing1, otherwise do thing2"
For example:
Let a = "It is raining"
This is clearly a boolean statement, it is either 'true' or 'false'. An example of some program may be
; Here the letter 'a' means "it is raining", like we said up above if (a) { put on poncho } else { take off poncho }
This can be read as
if (it is raining) { put on poncho } else { take off poncho }
If Statements
In programming, and particularly mIRC scripts, we use if statements like the example above for our control management.
They follow the format if (condition) { commands }
For example, consider the following alias:
alias ifexample { echo -atg Let's see... if ($true) { echo -atg Yep. This is true. } echo -atg See? }
In this example, $true is the 'condition' and the 'command' is "echo -atg Yep. This is true."
If you put this alias in the remote section of mIRC and type /ifexample in the command line, it will echo to you "Yep. This is true".
Now try changing $true to $false and notice that it will not say this is true.
Also notice that no matter what is inside the if statement, you will see "Let's see..." and "See?". These things are completely separate from the control statement and will not depend on the value of the stuff in parenthesis.
Well this is all well and good, but how does this help us do anything? The simple answer is operators. Operators are special words you put inside control statements which will tell you if something is true or false.
What if you don't know if it's raining, but you still want to put a poncho on if it is? You may wish to have
Let a = Is it raining? if (a) { put on poncho }
Let's make an alias that tells us what to do under different weather conditions named "weather." We will make it so that we can type /weather rain, and it will tell us to put a poncho on. Remember that if you do this, $1 will be "rain".
To do this we will need to learn the "==" operator. There are two equals here and you MUST use them both. This checks to see if two things are equal. For example:
a == b // This is $false a == a // This is $true
So in our weather alias we will need to check to see if $1 is equal to "rain", and if it is, we echo "Put on a poncho."
alias weather { if ($1 == rain) { echo -atg Put on a poncho. } }
We use some special terms to refer to the condition, condition = v1 operator v2. In this example, v1 is $1, and v2 is weather. operator is ==. This is how the operator list is formatted so you will need to know this. We can also use special operators $v1 and $v2 to return what was in those positions. For example we could do this:
alias weather { if ($1 == rain) { echo -atg Due to $v1, you should put on a poncho. } } And it would tell us "Due to rain, you should..." because $v1 is $1 which is "rain"
Some operators do not have a v2, for example "isupper" is $true if v1 is all upper case, and $false if it is not. For this you only need the text to check, v1.
on *:TEXT:*:#: { if ($1- isupper) { msg $chan STOP YELLING AT ME... } if ($1- islower) { msg $chan Speak up.... } }
This is an example of an event, and of using two control statements.
Negation
One more thing about operators.
Now we know how to check if text is all upper case, and if something equals something, but what if we want to check the opposite? What if we want to know if all the text is NOT upper case, or if something does NOT equal something. For this, we use the 'negation' or 'not' operator !.
The negative version of isupper is !isupper
on *:TEXT:*:#YellHouse: { if ($1- !isupper) { msg $chan YOU HAVE TO YELL IN #YELLHOUSE!!! } }
This detects if $1- is NOT all upper case. This is the same for most operators (!islower, !isvoice, !isin) the only exception is the equality operators. The opposite of == is !=, the opposite of > is <= and of < is >=, etc.
Combining Statements
Now what if you wish to check to things? Something like
if (it is raining AND I do not have a poncho on) { put on poncho }
This combines tow statements "it is raining", "I do not have a poncho on" using AND. We call AND a "conditional operator". There is also OR as you might imagine.
In mIRC, we use && for and, and || for or.
if (it is raining && I do not have a poncho on) { put on poncho }
So let's say we now want to modify our weather alias to remember what the last weather was, and not tell us to do something we've already done. For example /weather rain would tell us to put on a poncho, but if we did it again it wouldn't say that, because we already have one on.
alias weather { ; Check to make sure we actually told us of a weather, if not 'return' (which means STOP!) if ($1 != $null) { return } ; If it is now rain AND we didnt already know that if ($1 == rain && %weather != rain) { set %weather rain echo -atg Put on a poncho. } }
In this example we show that you can do two things in one control statement by putting them on different lines. Our if statement now checks to see if %weather is not already 'rain', that way we don't put the poncho on twice. We might also do this:
alias weather { ; Check to make sure we actually told us of a weather, if not 'return' (which means STOP!) if ($1 != $null) { return } ; If it is now rain AND we didnt already know that if ($1 == rain && %weather != rain) { set %weather rain echo -atg Put on a poncho. } if ($1 == clear && %weather != clear) { echo -atg Take off poncho set %weather clear } }
See Also
- If-Then-Else - 'Full' list of operators
- While - Another control structure for repeating things as long as something is true