mIRC Homepage

simple while loop fails

Posted By: ots654685

simple while loop fails - 05/04/09 11:25 AM

I created this while loop but for some reason it fails, it echo's all lines under it.

I thought this is the way to do a while loop?

alias imaptest {
var %i = 1
while (%i <= 10) {
if %i = 1 /echo -a 1
if %i = 2 /echo -a 2
if %i = 3 /echo -a 3
if %i = 4 /echo -a 4
if %i = 5 /echo -a 5
if %i = 6 /echo -a 6
if %i = 7 /echo -a 7
if %i = 8 /echo -a 8
if %i = 9 /echo -a 9
if %i = 10 /echo -a 10
inc %i
}
}

:update:
if ( %i == $null ) {
set %i 1
}
echo -a %i
if (%i <= 10) {
{
if %i == 1 /echo -a 1
if %i == 2 /echo -a 2
if %i == 3 /echo -a 3
if %i == 4 /echo -a 4
if %i == 5 /echo -a 5
if %i == 6 /echo -a 6
if %i == 7 /echo -a 7
if %i == 8 /echo -a 8
if %i == 9 /echo -a 9
if %i == 10 /echo -a 10
}
inc %i
if ( %i == 11 ) {
set %i 1
}
}
}

I changed to above but imo it can be much shorter?
At least it works now
Posted By: hixxy

Re: simple while loop fails - 05/04/09 12:24 PM

Of course it echoes all lines...

You're looping from 1 to 10, so all 10 of those if statements are going to be matched at some point in the loop.
Posted By: DJ_Sol

Re: simple while loop fails - 05/04/09 03:00 PM

alias imaptest {
var %i = 1
while (%i <= 10) {
if %i = 1 /echo -a 1
if %i = 2 /echo -a 2
if %i = 3 /echo -a 3
if %i = 4 /echo -a 4
if %i = 5 /echo -a 5
if %i = 6 /echo -a 6
if %i = 7 /echo -a 7
if %i = 8 /echo -a 8
if %i = 9 /echo -a 9
if %i = 10 /echo -a 10
inc %i
}
}

Because of your curly braces, this is your entire alias. You close the brackets here. The rest of the code you showed is doing nothing but taking up space confusing mirc.

Let's take a quick look at your while loop.

var %i = 1

while 1 is less than or equal to 10.
while (1 <= 10) {

if (%i = 1) echo -a 1
%i isn't 2 -10 so it compares false and gets to the inc.

Now %i = 2 which is less than or equal to 10.
%i = 2 so echo -a 2. Other comparisons are false so it incs %i.

Now %i is 3 which is less than or equal to 10.

It does this until the while comparison (%i <= 10) is false. Then it moves on past the while loop, but you have ended the alias with the closing curly brace so it stops.


How can it be shorter?

Well all you are doing is echoing what the variable is...

Code:
var %i = 1
while (%i <= 10) {
echo -a %i
inc %i
}
Posted By: ots654685

Re: simple while loop fails - 07/04/09 05:12 PM

well all those different echo's are different lines (it's for a /np script)

i have it working now , the only thing i would like to add is to have for example only %i = 3 to be echo'ed to #chan1 for example, and only when that chan is active also?

Get it?

is it even possible?
Posted By: RusselB

Re: simple while loop fails - 07/04/09 10:21 PM

Instead of using
Code:
echo -a %i
use
Code:
$iif(#channel == $active,echo -a %i)

Posted By: ots654685

Re: simple while loop fails - 08/04/09 03:12 PM

Mmm is it that simple, thanks

Is there also a else cmd which i ca use with $iif?
That way i can say something else if match failse.
Posted By: DJ_Sol

Re: simple while loop fails - 08/04/09 03:16 PM

/help $iif

That will tell you what you need to know.

Personally I only like to use $iif if it is neccesary. There are many times when an if,then,else statement is neccsary on one line.

It's quicker and mroe stable to say.

if (#chan = $active) do this
else do this

Or I'll set a variable.

var %x
if (compairson is true) %x = $ifmatch
else %x = $false
set %variable %x
© 2022 mIRC Discussion Forums