mIRC Home    About    Download    Register    News    Help

Print Thread
#225718 08/09/10 04:14 PM
Joined: Sep 2007
Posts: 202
F
firefox Offline OP
Fjord artisan
OP Offline
Fjord artisan
F
Joined: Sep 2007
Posts: 202
the manual says:

if (v1 operator v2) { commands }
elseif (v1 operator v2) { commands }
else { commands }

however a lot of scripts people write just say:

if (v1 operator v2) { commands }
if (v1 operator v2) { commands }
if (v1 operator v2) { commands }
if (v1 operator v2) { commands }
etc.

is it better to have this as:
if (v1 operator v2) { commands }
elseif (v1 operator v2) { commands }
elseif (v1 operator v2) { commands }
elseif (v1 operator v2) { commands }

even if no else at the end?

firefox #225725 08/09/10 04:47 PM
Joined: Jun 2007
Posts: 933
5
Hoopy frood
Offline
Hoopy frood
5
Joined: Jun 2007
Posts: 933
It's just a logical structure. You would use consecutive if-statements if multiple of those lines can be true as the same time. Consider this:

String: a b c d
if ($1 == a) command
if ($2 == b) command
if ($3 == c) command

Now consider a situation where you are only looking for a particular match:

String: a b c d
if ($1 == c) command
elseif ($2 == c) command
elseif ($3 == c) command

So it all depends on what you are doing. That said, there are quite a few scripts out there that use these statement incorrectly and only use if-statements where they should use one if-statement, followed by elseif-statements.

firefox #225729 08/09/10 05:38 PM
Joined: Oct 2004
Posts: 8,330
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
In addition, ELSE is not required. You can use it if you have a case where something should always happen if none of the IF/ELSEIF items are true. If you don't want anything to happen if those aren't true, then you don't need ELSE.

Many scripts, as mentioned, use IF's incorrectly (i.e. inefficiently). If you only use IF and not ELSEIF when you want only one item to match...

if ($1 == help) { command }
if ($1 == identify) { command }
if ($1 == quit) { command }

Then, mIRC has to check all of those even if the very first one is true. Obviously, if the first is true, the rest cannot be, so there's no reason to waste time checking them. If you used ELSEIF instead, mIRC doesn't have to check the comparisons.

Other scripts will use something like:

if ($1 == help) { command | halt }
if ($1 == identify) { command | halt }
if ($1 == quit) { command | halt }

That will work like ELSEIF's will and may even be slightly faster, but prevents adding any other script below those IF's. In most cases, you should just use ELSEIF. Besides, HALT in itself is often misused. In many cases, you should really use RETURN instead of HALT to stop a script as HALT can stop more than what you mean to stop.


Invision Support
#Invision on irc.irchighway.net
Riamus2 #225749 08/09/10 08:38 PM
Joined: Sep 2007
Posts: 202
F
firefox Offline OP
Fjord artisan
OP Offline
Fjord artisan
F
Joined: Sep 2007
Posts: 202
ok thanks guys


Link Copied to Clipboard