Basic control tutorial

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

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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. } 
}

See Also

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