|
Return To Main MIRC Menu If-Then-ElseAt some point in scripting you need to make mIRC compare one value with another. This might be to see if a word in text matches a word you want to check for. It might be to check if a certain time has been reached. It could be many things. What it mostly is though is a way of making mIRC do a little thinking. The IF statement makes mIRC ask itself if one thing is equal to, or different from, more or less than another thing. When we use the IF statement we are teaching mIRC to ask 'is it?'. Comparing two or more values is essential to any decision making. The human brain works out how tired or hungry or whatever we are by comparing our current state to other conditions. Our minds tell us we are hungry when it finds our current state of hunger is higher than the state we like it to be, etc. The mIRC If Statement mIRC uses the IF statement to compare values. if (<value1> <operator> <value2>)
{ <commands> } The () brackets are not always essential, but do help mIRC to work out which are the values you want compared. The {} brackets are, as covered in the MIRC Aliases page, able to contain multiple commands if a new line is used for each command. <value1> is the first value you want mIRC to look at, while <value2> is the value you want mIRC to compare it to. The <operator> parameter is the kind of comparison you want mirc to make.
There are a few more operators in mIRC's If-Then-Else statements. Read the mIRC help files to study them if you wish. The ones above are more than enough for our purposes however. I have not listed them for you to learn them. I do not expect you to remember them all. I have detailed the operators purely for you to get an idea of the kind of comparisons mIRC can make. All you need to remember is that mIRC scripts can do many comparisons of values. If-Then, the actionI will show some examples in a moment to demonstrate exactly what use the IF statement can be. First however we need to cover the 'Then' part. if (<value1> <operator> <value2>)
{ <commands> } if (<value1> == <value2>) { <commands>
} Now its time for a few examples to help ensure that the principles of the IF statement are clear. if ($me != %favenick) { /nick %favenick } if ($time == 10:00:00) { /echo -a Its Ten O'clock
} If-Then-ElseThe Else follows one or more If statements, and provides an instruction to be followed when the If statement was untrue. IF if this comparison is true You do not have to have an Else at the end of If statements but it is often useful to have one. The Else statement is similar to an If statement but needs no values or operators. ELSE { <commands> } Example: Don't be alarmed by the length of the silly script above. The first line opens a dialog box for you to answer a question and stores your answer as a variable called %answer. The first If statement looks to see if the answer given was the word 'yes'. If it was it echoes a comment to the screen and sets a variable called %likesburgers to record that you do like burgers. The second If statement looks to see if your answer was no. If your answer was no it will echo the result to the active window, and save the variable %likesburgers with a value of No. The Else statement looks to see if neither of the If statements were true, in which case you must have typed something other than yes or no. It echoes this information to the screen and sets a different variable called %unsure with a value of yes. Its a silly script and not one I recommend using but it does illustrate the way multiple commands may be issued. There is one other statement type in the If-Then-Else group, the ELSEIF statement. A second If statement will check its values regardless of whether or not the first If statement was true. An ELSEIF statement is just like an If statement but will only check the values if the preceeding statement was not true. To check a second pair of values only if the first statement were true there are at least two methods. One is to put another If statement INSIDE the command area (between the {} brackets). The other is to reverse the question of the first statement so that you ask the exact opposite, and then use an elseif statement. For example, to determine if a number is between 1 and 10 either of the following scripts would do the job: script 1: script 2: Those two scripts both attain the same result despite using different methods and either would suffice for the purpose.
Back / Index / Next ©1999 The Black Knight
|
|
|||||||||||||||||||||||||||||||||
Copyright ©2001 MIRC
Workshop Search engine optimisation positioning tutorials| Internet Marketing Consultant |
Back To Top |