Difference between revisions of "Basic control tutorial"
m |
m ('Final' first draft) |
||
Line 76: | Line 76: | ||
Now what if you wish to check to things? Something like | 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 } | if (it is raining AND I do not have a poncho on) { put on poncho } | ||
− | This combines | + | This combines two 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.<br /> |
In mIRC, we use && for and, and || for or. | In mIRC, we use && for and, and || for or. | ||
if (it is raining && I do not have a poncho on) { put on poncho } | if (it is raining && I do not have a poncho on) { put on poncho } | ||
Line 105: | Line 105: | ||
} | } | ||
+ | ==Nesting== | ||
+ | This is a bit obvious, but warrants saying aloud. Since 'if' is technically a command, we can 'nest' it. That means we can have if (condition1) { command | if (condition2) { command2 } } <br /> | ||
+ | For example, now we want /weather clear to say "Take off poncho" if %weather is rain. | ||
+ | |||
+ | 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) { | ||
+ | ; Here we NEST another if statement! | ||
+ | if (%weather == rain) { | ||
+ | echo -atg Take off poncho | ||
+ | } | ||
+ | set %weather clear | ||
+ | } | ||
+ | } | ||
+ | |||
+ | Another example of this being used more constructively, and in an event: | ||
+ | on *:TEXT:*:#YellHouse: { | ||
+ | if ($1- !isupper) { | ||
+ | msg # YOU HAVE TO YELL IN #YELLHOUSE!!!! | ||
+ | if ($right($1-,1) == !) { | ||
+ | msg # PUNCTUATION IS NOT GOOD ENOUGH!!! | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | ==Else and Elseif== | ||
+ | Elseif and Else are two very useful commands. These commands have to be used with an 'if' statement. Elseif is only considered if the if statement before it was $false, and else is only considered if everything before it was $false. | ||
+ | |||
+ | if (it is raining) { put on poncho } | ||
+ | elseif (it is snowing) { put on jacket } | ||
+ | else { take off poncho and jacket and enjoy the sun } | ||
+ | |||
+ | Notice, else does not take a condition! Else statements run if none of the elseif statements above it have run and if the closest if has not run. In this example, if it were somehow both raining and snowing, we would only put on a poncho, not a jacket, because the first if would run, and the elseif would not. | ||
+ | alias Sign { | ||
+ | var %a = $1 | ||
+ | if (%a > 0) { echo -atg Positive! } | ||
+ | if (%a < 0) { echo -atg Negative! } | ||
+ | else { echo -atg Zero! } | ||
+ | } | ||
+ | |||
+ | There is an error in this alias, and that is that if $1 is positive, it will echo "Positive!" and "Zero!". This is because else only looks at the first if above it. The proper way to do this is: | ||
+ | alias Sign { | ||
+ | var %a = $1 | ||
+ | if (%a > 0) { echo -atg Positive! } | ||
+ | elseif (%a < 0) { echo -atg Negative! } | ||
+ | else { echo -atg Zero! } | ||
+ | |||
+ | Modification of the weather alias to make it faster by using elseifs. This increases speed because it doesn't have to check $1 if it already knows it is rain | ||
+ | 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. | ||
+ | } | ||
+ | ; We use an elseif here, so that it doesnt bother checking if $1 == rain. | ||
+ | elseif ($1 == clear && %weather != clear) { | ||
+ | ; Here we NEST another if statement! | ||
+ | if (%weather == rain) { | ||
+ | echo -atg Take off poncho | ||
+ | } | ||
+ | set %weather clear | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | ==Other Boolean Values== | ||
+ | There are three important notes about boolean values. Besides $true and $false, three other things are interpreted | ||
+ | * 0 is ALWAYS interpreted as $false [if (0) { this will never happen }] | ||
+ | * $null is ALWAYS interpreted as $false [if (!$1) { this is like saying if $1 is not null, because its !$false if $1 is $null }] | ||
+ | * Anything else is ALWAYS interpreted as $true | ||
+ | <br /> | ||
+ | <br /> | ||
+ | Consider this minor change to the weather alias where we change if ($1 == $null) to something a bit shorter. This uses the fact that $null is $false | ||
+ | alias weather { | ||
+ | ; Check to make sure we actually told us of a weather, if not 'return' (which means STOP!) | ||
+ | if (!$1) { [[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) { | ||
+ | ; Here we NEST another if statement! | ||
+ | if (%weather == rain) { | ||
+ | echo -atg Take off poncho | ||
+ | } | ||
+ | set %weather clear | ||
+ | } | ||
+ | } | ||
+ | ==Examples== | ||
+ | Completed weather alias: | ||
+ | alias weather { | ||
+ | ; Check to see if they gave us a weather condition, and if it isnt the same as before | ||
+ | if ($1 && $1 != %weather) { | ||
+ | if ($1 == rain) { | ||
+ | echo -atg Put on a poncho! | ||
+ | } | ||
+ | elseif ($1 == snow) { | ||
+ | echo -atg Put on a jacket! | ||
+ | } | ||
+ | elseif ($1 == clear) { | ||
+ | echo -atg Enjoy the sun! | ||
+ | } | ||
+ | ; If this is some nonsense weather | ||
+ | else { | ||
+ | echo -atg I don't care about $1 $+ , I only advise on rain snow and clear. | ||
+ | return | ||
+ | } | ||
+ | ; This chain of control is for checking what to remove | ||
+ | if (%weather == rain) { echo -atg Take off poncho. } | ||
+ | elseif (%weather == snow) { echo -atg Take off jacket. } | ||
+ | set %weather $1 | ||
+ | |||
+ | } | ||
+ | ; If they didnt give us a condition... | ||
+ | elseif (!$1) { echo -atg What is the weather like? } | ||
+ | ; If its the same as before, tell them we know! | ||
+ | else { | ||
+ | echo -atg I already told you what to do about the $1 $+ ... | ||
+ | } | ||
+ | } | ||
==See Also== | ==See Also== |
Latest revision as of 00:50, 18 November 2010
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.
Contents
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 two 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 } }
Nesting
This is a bit obvious, but warrants saying aloud. Since 'if' is technically a command, we can 'nest' it. That means we can have if (condition1) { command | if (condition2) { command2 } }
For example, now we want /weather clear to say "Take off poncho" if %weather is rain.
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) { ; Here we NEST another if statement! if (%weather == rain) { echo -atg Take off poncho } set %weather clear } }
Another example of this being used more constructively, and in an event:
on *:TEXT:*:#YellHouse: { if ($1- !isupper) { msg # YOU HAVE TO YELL IN #YELLHOUSE!!!! if ($right($1-,1) == !) { msg # PUNCTUATION IS NOT GOOD ENOUGH!!! } } }
Else and Elseif
Elseif and Else are two very useful commands. These commands have to be used with an 'if' statement. Elseif is only considered if the if statement before it was $false, and else is only considered if everything before it was $false.
if (it is raining) { put on poncho } elseif (it is snowing) { put on jacket } else { take off poncho and jacket and enjoy the sun }
Notice, else does not take a condition! Else statements run if none of the elseif statements above it have run and if the closest if has not run. In this example, if it were somehow both raining and snowing, we would only put on a poncho, not a jacket, because the first if would run, and the elseif would not.
alias Sign { var %a = $1 if (%a > 0) { echo -atg Positive! } if (%a < 0) { echo -atg Negative! } else { echo -atg Zero! } }
There is an error in this alias, and that is that if $1 is positive, it will echo "Positive!" and "Zero!". This is because else only looks at the first if above it. The proper way to do this is: alias Sign {
var %a = $1 if (%a > 0) { echo -atg Positive! } elseif (%a < 0) { echo -atg Negative! } else { echo -atg Zero! }
Modification of the weather alias to make it faster by using elseifs. This increases speed because it doesn't have to check $1 if it already knows it is rain
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. } ; We use an elseif here, so that it doesnt bother checking if $1 == rain. elseif ($1 == clear && %weather != clear) { ; Here we NEST another if statement! if (%weather == rain) { echo -atg Take off poncho } set %weather clear } }
}
Other Boolean Values
There are three important notes about boolean values. Besides $true and $false, three other things are interpreted
- 0 is ALWAYS interpreted as $false [if (0) { this will never happen }]
- $null is ALWAYS interpreted as $false [if (!$1) { this is like saying if $1 is not null, because its !$false if $1 is $null }]
- Anything else is ALWAYS interpreted as $true
Consider this minor change to the weather alias where we change if ($1 == $null) to something a bit shorter. This uses the fact that $null is $false
alias weather { ; Check to make sure we actually told us of a weather, if not 'return' (which means STOP!) if (!$1) { 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) { ; Here we NEST another if statement! if (%weather == rain) { echo -atg Take off poncho } set %weather clear } }
Examples
Completed weather alias:
alias weather { ; Check to see if they gave us a weather condition, and if it isnt the same as before if ($1 && $1 != %weather) { if ($1 == rain) { echo -atg Put on a poncho! } elseif ($1 == snow) { echo -atg Put on a jacket! } elseif ($1 == clear) { echo -atg Enjoy the sun! } ; If this is some nonsense weather else { echo -atg I don't care about $1 $+ , I only advise on rain snow and clear. return } ; This chain of control is for checking what to remove if (%weather == rain) { echo -atg Take off poncho. } elseif (%weather == snow) { echo -atg Take off jacket. } set %weather $1 } ; If they didnt give us a condition... elseif (!$1) { echo -atg What is the weather like? } ; If its the same as before, tell them we know! else { echo -atg I already told you what to do about the $1 $+ ... } }
See Also
- If-Then-Else - 'Full' list of operators
- While - Another control structure for repeating things as long as something is true