mIRC Home    About    Download    Register    News    Help

Print Thread
#181698 29/07/07 02:20 PM
Joined: Jan 2004
Posts: 509
L
Fjord artisan
OP Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
If I had something like
Code:
on *:text:*:#: {
  if () {
    if () { }
    elseif () { }
    }
  elseif () {
    if () { }
    elseif () { }
    }
  ELSEIF () { }
  elseif () { }
}


The last elseif won't trigger, such that I have to change the CAPS one to if..

Especially if you have something like:

Code:
on *:text:*:#: {
  if () { }
  elseif () { }
  while () { }
  set %this
  set %that
  This has to be if and not elseif () { }
  elseif { }
}


Anything after a while has to be elseif?

What I had before was:

Code:
on *:text:*:#: {
  if () {
    if () { }
    if () { }
    }
  if () {
    if () { }
    if () { }
    }
  if () { }
  if () { }
  }
}


And changed it to.

Code:
on *:text:*:#: {
  if () {
    if () { }
    elseif () { }
  elseif () {
    if () { }
    elseif () { }
  elseif () {
  elseif () { }
  }
}


Which blocked some of my elseif, making me change some of them to ifs.

And as you must know, they are completely on text event scripts, such as storing varibles, adding up numbers, checking for words, etc.

Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
As far as I know, there is no limit to how many elseif's you can use in a row. If some of your elseif's aren't triggering it is because, either a previous if/elseif already triggered, or the elseif in question doesn't match the conditions you've given to it.

Example:

var %a = 3
if (%a == 1) { }
elseif (%a == 2) { }

elseif (%a == 3) { }
elseif (%a == 4) { }
elseif (%a == 5) { }


In this example, the blue lines have no match, so they are not executed; the green line matches, so it is executed; the red lines are never checked, because the green line already matched above.
Blue = NO match
Green = MATCH
Red = Ignored

Example:
var %a = 6
if (%a == 1) { }
elseif (%a == 2) { }
elseif (%a == 3) { }
elseif (%a == 4) { }
elseif (%a == 5) { }


In this example, all of the blue lines have no match, so they aren't executed.

-genius_at_work

Joined: Jan 2004
Posts: 509
L
Fjord artisan
OP Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
Okay, so my understand is once an elseif is reached, it will do a /halt, whereas if it was if, it will continue on?

if (a == a) { do this }
if (a == a) { do that }

Will do this and that.

Whereas.

elseif (a == a) { do this }
elseif (a == a) { do that }

Will stop at this, and not do that?

Alright thanks I'll look into it.

-Neal.

Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
The if statement does NOT do a /halt internally.


if-elseif-else statements operate in clusters. Each cluster begins with a single IF (required), which can then be followed by 1 or more ELSEIF (optional), which can then be followed by a single ELSE (optional).

Example pseudocode:

Code:
action 1
action 2

IF (condition 1) { action 3a }
ELSEIF (condition 2) { action 3b }
ELSEIF (condition 3) { action 3c }
ELSE { action 3d }

action 4
action 5


In this script, action 1 and action 2 always happen. If condition 1 is true, action 3a happens and then the script skips ahead to action 4. If condition 1 is false, and condition 2 is true, action 3b happens and then the script skips ahead to action 4. If conditions 1 and 2 are false, and condition 3 is true, action 3c happens and the script skips ahead to action 4. If conditions 1 and 2 and 3 are false, action 3d happens and the script continues to action 4. Action 5 then happens.

In this case, the first match in the IF cluster causes the script to jump to the end of that cluster.



Example pseudocode:

Code:
action 1
action 2

IF (condition 1) { action 3 }
IF (condition 2) { action 4 }
IF (condition 3) { action 5 }

action 6
action 7


In this script, action 1 and action 2 always happen. If condition 1 is true, action 3 happens. If condition 2 is true, action 4 happens. If condition 3 is true, action 5 happens. Action 6 and action 7 then happens.

In this case, the IF statements are independent of each other, so if one matches, it doesn't affect the others.

-genius_at_work

Joined: Jan 2004
Posts: 509
L
Fjord artisan
OP Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
So would this be true.

if (a == a) && (b == b) && (c == c) { do this }
elseif (so a minimum of 1 of the 3 is not true?)

And would using elseif in this case be true?

if (a == a) && (b == b) && (c == c) { blah }
elseif (a != a) && (b == b) && (c == c) { blah }

What would you use for the red? Elseif? (In terms of speed).

Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
If and Elseif are control structures designed to operate the flow of commands.

If you have one value (%a) and there are multiple possible values it could be, and you want different actions to happen if it's different values, you use if-elseif-elseif to do the checking.

If you are checking completely different variables for completely diferent things, you'd use separate if statements.

So using your example, take a look at this code:

  1. if (%a == a) && (%b == b) && (%c == c) { blah }
  2. elseif (%a != a) && (%b == b) && (%c == c) { blah }
  3. elseif (%a == a) && (%b != b) && (%c == c) { blah }
  4. elseif (%a == a) && (%b == b) && (%c != c) { blah }
  5. else { blah }
With that code in mind, lets consider the following scenareos:
  • /var %a = a, %b = b, %c = c -> This will hit 1
  • /var %a = b, %b = b, %c = c -> This will hit 2
  • /var %a = a, %b = a, %c = c -> This will hit 3
  • /var %a = a, %b = b, %c = a -> This will hit 4
  • /var %a = b, %b = c, %c = c -> This will be caught by 5
5 will catch all other possible variations of the variables not being one of the listed value sets.

Now, you were wondering about speed. There's two considerations.

if you just used 'if' for all 5 lines, it would be slower for this example as it would check all 4 if's every time though, and wouldn't skip out of the structure if it hit earlier.

As with genius_at_work's earlier example, the red lines would never get evaluated as it matched an earlier line. This is the point of an if-elseif-else structure.

How about a real world example? This is from my remotes:

Code:
ON *:CONNECT: {
  if ($istok(rizon irchighway chatspike darkernet, $network, 32) && ($me == $mnick)) {
    .msg nickserv identify <password>
    if ($network == rizon) {
      .mode $me -x
      .timer 1 10 .join -n #chan1,#chan2,#chan3
      .timer 1 11 .join -n #chan4,#chan5,#chan6
      join -n #chan7,#chan8,#chan9
    }
    elseif ($network == irchighway) {
      .timer 1 5 .mode $me -r
      .join -n #chan1,#chan2,#chan3
      .timer 1 30 .join -n #chan4,#chan5,#chan6
    }
    elseif ($network == chatspike) {
      .join -n #chan1
    }
  }
}

It should be fairly straight forward what this means.

Joined: Jan 2004
Posts: 509
L
Fjord artisan
OP Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
Okay good, elseif knows when to stop.

Code:
1.if (this)
    if (that) nope
    elseif (this) yes!
2.if something else
3.if something else


There's only 1 elseif in the code, if that elseif is triggered, will if2 and if3 be triggered?

Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
Hrm.. It's difficult to tell.. But given the identation, I'd assume you mean:

Code:
1. if (this) {
2.   if (that) { nope }
3.   elseif (other) { yes! }
4. }
5. if (something) { boo }
6. if (somethingelse) { blah }

The if's on both line 5 and 6 will be executed.

When nesting if statements, using the { } are important to get the expected structure right.

Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
Yes, your 2nd & 3rd if statements would be checked, under almost any circumstances. There are two exceptions that I'm aware of.
1) The /halt command is encountered before getting to those statements.
2) The /return command is encountered before getting to those statements.

Joined: Jan 2004
Posts: 509
L
Fjord artisan
OP Offline
Fjord artisan
L
Joined: Jan 2004
Posts: 509
Alright thanks Lessy and RusselB.


Link Copied to Clipboard