If-Then-Else Statements, While Loops, and Goto Loops.
 
Post new topic   Reply to topic    Hawkee.com Forum Index -> mIRC Tutorials
 
CaptainHollyShort  -  Fri May 26, 2006 7:16 am Reply with quote
If-Then-Else
If-Then-Else staements are some of the most commonly used pieces of code in mIRC and are used when you want to control how a script proceeeds. There are three things in an if or elseif statement; v1 and v2, the items you are comparing, and the operator, which you compare v1 and v2 with. You can nest as many if statements and elseif statements inside each other. EXAMPLE:

Let's say you wanted to Send a message to your channel with Heads or Tails at random. You could do this using if-then-else
Code
 

The first if statement compares v1 and v2 (%coin and 1) by using ==, which means "is equal to". If %coin is equal to 1, the bracketed text happens. The second line happens if the first if doesn't. It says Elseif %coin is equal to 2. Then it ends.

NOTE: You don't have to use braces and parenthesis for one-line if steatements, but it makes life easier. mIRC also reads = the same as ==.

Now for a more complex example.
Code
 

Line 1 starts an if statement. Lines 2 and 3 are nested inside the first if statement. Line 3 is a special if statement that only triggers if the if before it does not. Line 5 is an else statement, that happens only if the ifs and elseifs in an event DO NOT occur.

$iif
$iif() is an identifier that returns text based on if something is true or false.
Code
 

What this means, is if v1 and v2 are true based on the operator you provide, it returns true, else it will return false. EXAMPLE:
Code
 


The first line of code uses $iif to set a variable known as %t.or.f, which stands for true or false. After that, the if and elseif statements proceed as normal until the last } brace is found.

Operators
Operators are what is used to compare v1 and v2 in an if statement, $iff(), or while loops. Some popular operators are:
Code
 

There are many more operators listed under /help if then else in your mIRC help file. I simply cannot be bothered to list all of them here, especially the more obscure ones.Also, prefixing an operator with ! negates it. EXAMPLE: !ison means is v1 is NOT on $chan ($chan is v2).

&& and ||
&& and || are used as logic operators. Logic operators are used between if statements and while loops and mean AND or OR. EXAMPLE:
Code
 

The first if statment checks if $2 equals eat and $3 equals bananas. The elseif statement checks if $2 is eat OR sleep.

While Loops
While loops are exactly like if statements except they happen until the while statement is no longer true. they take the same format as if statements but use while instead of if or elseif. EXAMPLE:
Code
 

As you can see while %while is less than 11, the loop will keep increasing %while until it reaches 10, then unset %while. The same operators in if statements work here. You can also nest while loops inside each other. EXAMPLE:
Code
 

This script took an extra (although in practice redundant) step which checks if the number is also less than 10.

Goto Loops
Goto loops are loops where the /goto command is used to go back to a point in a script prefixed with :. An example would be :goto. EXAMPLE:
Code
 

The script above Checks if %x is less than 11. If it is, the /goto command sends it back to :goto. Although longer than while loops or if statements, goto loops can be useful and do not require operators, and cannot be nested inside each other.

Putting it all together!
Code
 


As we can see, this is a simple ban/kick script. See if you can use your new knowledge to figure out what parts of the script do what.

If you learned something from this, congratulations! Maybe I'll write another tutorial sometime. :D

-VHS

EDIT: I fixed the operator, added a few more examples, and tried my best to clearly explain this subject more. If there are still problems, let me know.


Last edited by CaptainHollyShort on Sat May 27, 2006 3:44 am; edited 1 time in total
Acid-Religion  -  Fri May 26, 2006 11:06 am Reply with quote

CaptainHollyShort wrote


$iif
$iif() is an identifier that returns text based on if something is true or false.
Code
 

What this means, is if v1 and v2 are true, it returns true, else false.EXAMPLE:
Code
 


Found this part a bit confusing. Should clearify v1, v2 and operator is. You should also explain the example code.

CaptainHollyShort wrote


Operators
Operators are what determine what happens in an if statement, $iff(), or while loops. Some popular operators are:
Code
 


Highlited a little error here...
CaptainHollyShort  -  Fri May 26, 2006 12:01 pm Reply with quote
I dont see an error. >>;
KuTsuM  -  Fri May 26, 2006 1:47 pm Reply with quote
?!= - Equal to?


I've never heard that on before. I'm guessing the author of the mIRC help file hasn't either...

mIRC Help File wrote

The Operators

== equal to
=== equal to (case-sensitive)
!= not equal to
< less than
> larger than
>= larger than or equal to

<= smaller than or equal to
// v2 is a multiple of v1
\\ v2 is not a multiple of v1
& bitwise comparison

isin string v1 is in string v2
isincs string v1 is in string v2 (case sensitive)
iswm wildcard string v1 matches string v2
iswmcs wildcard string v1 matches string v2 (case sensitive)
isnum number v1 is a number in the range v2 which is in the form n1-n2 (v2 optional)
isletter letter v1 is a letter in the list of letters in v2 (v2 optional)

isalnum text contains only letters and numbers
isalpha text contains only letters
islower text contains only lower case letters
isupper text contains only upper case letters

ison nickname v1 is on channel v2
isop nickname v1 is an op on channel v2
ishop nickname v1 is a halfop on channel v2
isvoice nickname v1 has a voice on channel v2
isreg nickname v1 is a normal nick on channel v2
ischan if v1 is a channel which you are on.
isban if v1 is a banned address in internal ban list on channel v2

isaop if v1 is a user in your auto-op list for channel v2 (v2 optional)
isavoice if v1 is a user in your auto-voice list for channel v2 (v2 optional)
isignore if v1 is a user in your ignore list with the ignore switch v2 (v2 optional)
isprotect if v1 is a user in your protect list for channel v2 (v2 optional)
isnotify if v1 is a user in your notify list.


Apparrently you forgot a lot of operators. I also agree with Acid-Religion, your descriptions of the if-than-else and $iif are very vague, and to someone just learning this concept is probably of no help whatso ever.
LIQUID_NiTrO  -  Wed Sep 12, 2007 8:20 am Reply with quote
Also you forgot to mention that an ! can preceed any operator or identifier to mean "not", ex !isalpha would mean "is not an alphabetical string".

And idk if you knew, but your "loops in loops" example will hang...it will increment %while up until 10, at which point the outer loop will reiterate infinitely because it contains no increment command.

And adding on to the previous post regarding $iif.. $iif is a more useful identifier than you've made it out to be. It not only returns text, I frequently use it to determine which command should be used. For example, if you're making an alias which can be called either as an identifier or a command, you probably want to echo the final result when it's executed as a command, but when it's executed as an identifier, you want to return the final result. Here's your solution:
$iif($isid,return,echo -a) result_here
rAnk  -  Sat Aug 09, 2008 12:33 pm Reply with quote
Very good tutorial.
Display posts from previous:   
Post new topic   Reply to topic    Hawkee.com Forum Index -> mIRC Tutorials All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.