It's obvious, that you'd be doing an incremental loop (start with %x = 1), if you do while ($chan(%x)) which will end when $chan(%x) is $null, meaning %x's value is one unit higher than the total amount of channels.

The only thing that matters is that you minimize the number of times the identifier is called. Like you rightfully pointed out, it's better to put the value of $chan(0) into a variable, and reference that, instead of each time making mIRC retrieve the number of channels you're on.

Aside from that, whether you loop like:

var %x = 1
while ($gettok($1-,%x,32) != $null) {
; do something with $v1
inc %x
}

or

var %x = 1, %y = $0
while (%x <= y) {
; do something with $gettok($1-,%x,32)
inc %x
}

Doesn't really make any difference. Although $gettok is referenced once more in the one where it is in the while condition, on the other hand, it doesn't have to assign $0 to a variable like in the second example. Which one you choose will certainly not be based on efficiency/speed issues, as they are of no importance here.

Cases where you'll want to be against the putting of the identifier in a while condition is those that could be avoided, and should be avoided because of the inefficiency they represent. Examples of these are: $len() or $lines(). Instead of doing something like while (%x <= $lines(file.txt)) it is advised to put the value of $lines() into a var and compare %x to this. For $lines() to work, it must open the file (this means disc access) and check how many lines there are. This is far more intensive than checking the value of a variable.

Thus here, calling $lines more than once is unjustified, and should be avoided. In the case of the $gettok, or in this thread $chan(%x) you are going to need to retrieve the value of that identifier with each iteration anyway, so whether you put it in the while condition or reference it inside the body really doesn't matter.


Gone.