Basic control tutorial

From Scriptwiki
Revision as of 00:00, 18 November 2010 by Aca20031 (talk | contribs)

Jump to: navigation, search

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

if (a) { 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.

See Also

  • If-Then-Else - 'Full' list of operators
  • While - Another control structure for repeating things as long as something is true