mIRC Homepage
Posted By: SubSpace for statement - 24/01/03 11:16 PM
I know there is a while statememt, but every now and then (when I'm still waking up) I forget an 'inc %i' statement that would never happen if it were in some for-like syntax.

Result: aborted DCC sends smirk
Posted By: theRat Re: for statement - 25/01/03 01:50 AM
solutions:
1. Don't forget the 'inc %i'
2. Create scripts on second instance of mIRC
3. Don't use DCC while creating scripts
4. Don't use DCC at all
5. Don't create scripts when you're still waking up
... laugh
Posted By: SubSpace Re: for statement - 25/01/03 02:01 AM
6. Just add the <censored> for statement wink
Posted By: Sabby Re: for statement - 25/01/03 04:29 AM
hm, i too would like the for() inscripted in mIRC, though some people wont use it because mIRC has while() and :loops..

but still never hurts to have more options.
Posted By: Gon_ Re: for statement - 25/01/03 04:41 AM
I would like this too. A FOR loop uses less code than a WHILE loop. That is an advantage. A 1 line FOR loop can be more elegant than a WHILE loop, imo, that is good too. There is also a chance that it *might* be faster than a WHILE loop, because your not using an interpreted script command to inc it, mIRC would do that internally. Im not trying to justify this addition on possible speed gain though, there might be none. It's nice to add new things to essence of the scripting language. A for loop is preferable to some, and save you a few bytes in code, so why not?
Posted By: codemastr Re: for statement - 25/01/03 06:24 PM
:loops are horrible horrible horrible! They have been shunned in just about every language they exist in. 'goto' is a dreaded operation in just about every language. Most languages have multiple ways to create a loop (each designed to make code more readable in certain situations). A while() loop is useful when no initialization is needed and a non-standard iteration, for example
while (%i != 4)
{
echo %i
if (%i == 2) {
inc %i 2
}
else {
inc %i
}
}

Notice how depending on the value of %i, the iteration is done differently.

For loops are different, they are used when initialization is needed and the iteration is standard:

for (%i = 0; %i != 4; inc %i)
{
echo %i
}

Then do while loops are used when you want to execute the body of the loop before checking the conditional:

do {
echo %i
inc %i
} while (%i != 4)

Those are the standard ones, but some languages even go farther than that. For example foreach which returns an entry in a list (useful for mIRC tokens)

foreach %tok ($1-) {
echo %tok
}

that would echo the value of $1, $2, etc. Could even be expanded say

foreach %tok ($1-,32) {
echo %tok
}

where the 32 (space) is the token to seperate by.

Then there are less common loops (primarily in perl). For example perl has an until() loop which continues until the expression evaluates to true, (rather than a while which waits till it evaluates to false). It also has do {} until() which does the same thing as until() except it follows the rules of do {} while(). And from what I'm told the newest versions of perl even go one step further and have a loop called loop {} which basically creates an infinite loop (same as doing while(1) { }).

Ok now I agree implementing all of those would be overkill, but my point is simply saying "there are other ways to create a loop" wasn't good for any of the other languages out there, so why should that be a good enough excuse for mIRC?
Posted By: Sabby Re: for statement - 25/01/03 08:33 PM
actually :loops can be very useful

if you use this kind of scripting style, for example dialogs:

on *:dialog:whatever:sclick:*:{
goto $did

:1
blah

:2
blah2

:3
blah3
}
Posted By: codemastr Re: for statement - 25/01/03 08:41 PM
I didn't say they aren't useful, I said that they are bad. There is a difference. What you just did there would be much better suited with a switch() statement, which IMHO should also be added to mIRC.
Posted By: Sabby Re: for statement - 25/01/03 10:59 PM
agreed
Posted By: BoredNL Re: for statement - 26/01/03 03:04 AM
on *:dialog:whatever:sclick:*:{
goto $did

:1
echo ok, it's 1

:2
echo ok, it might be 2.. or 1.. who knows?

:3
echo hmm.. It might be 2, 3, or 1? I see no end to this!

:4
echo I'm confused now, please leave me alone.

:5
echo AHH!!

}
Posted By: Sabby Re: for statement - 26/01/03 03:23 AM
huh?
Posted By: Gon_ Re: for statement - 26/01/03 03:56 AM
Quote:

actually :loops can be very useful

if you use this kind of scripting style, for example dialogs:

on *:dialog:whatever:sclick:*:{
goto $did

:1
blah

:2
blah2

:3
blah3
}


That isn't a loop though, that is just a GOTO jump wink

GOTO is very useful yes. But using it for looping isnt as fast as a WHILE loop in mIRC.
Posted By: Sabby Re: for statement - 26/01/03 04:04 AM
ok, i posted that because i thought he ment that :stuff isnt usefull.. now i see what he means by like

var %i = 1
:blargh
echo -a %i : $chr(%i)
if (%i <= 255) goto blargh
else return
Posted By: SergioNL Re: for statement - 26/01/03 06:04 AM
just do CTRL + pause/brake if that exidently happens and fix your bug afterwards(it wil show exactly wich line it stopped)
Posted By: Hammer Re: for statement - 26/01/03 08:58 AM
Quoting codemastr talking about GOTO:

Quote:

I didn't say they aren't useful, I said that they are bad (emphasis added). There is a difference.


Wrong. GOTO is not inherently bad. It has been abused over the years rather dramatically, leading to spaghetti code that's all but impossible to follow, that's true enough. Blaming that on GOTO is ridiculous. Blame the ignorant or just plain poor coder(s), not the inappropriate use of a language feature.

I assume that you've been coding for more than 12 minutes. Think about error trapping. I will grant you that I can't think of a reason offhand to use goto instead of calling an error alias that halts the script. Perhaps if there was some necessary cleanup for a graceful exit that would preclude using a central alias.
Posted By: Gon_ Re: for statement - 26/01/03 10:50 AM
I think Codemastr meant that GOTO was bad for looping, and not that all uses of GOTO are bad.
For instance, i can't think of 1 single advantage a GOTO loop has over a WHILE loop, other than backwards compatability with old mIRC versions.
Posted By: Hammer Re: for statement - 26/01/03 12:16 PM
I quite agree with him on that: GOTO is inferior to WHILE for looping. I also agree that 90+% of the code that I've seen in mIRC script that uses GOTO in it is badly scripted (from many points of view). My only objection to what he said was that GOTO, in and of itself, is "bad."
Posted By: Doqnach Re: for statement - 26/01/03 02:52 PM
GOTO loops are very usefull with initialisation of scripts...

you ask for inputs, and check afterwards if they add up, if not you have the script return...

there is nothing a for loop can do what a "var %i = 1 | while (%i <= 10) { bla | inc %i }" can't do... just a little more code. imo mIRC script should stay easy, and not become a coding language on itself
Posted By: scoob Re: for statement - 26/01/03 03:57 PM
Quote:
GOTO is very useful yes. But using it for looping isnt as fast as a WHILE loop in mIRC.


this isn't 100% true, any and all work I've done listing to a @window using echo and/or aline has been noticeably quicker using goto loops.
Posted By: SubSpace Re: for statement - 26/01/03 05:53 PM
GOTO for error handling is appalling, any time, any day..
In fact, using GOTO for anything I can think of is pretty bad coding.

GOTO is the statement that is removed from every self respecting higher level language, or should be.

And regarding spaghetti code, it will come to no surprise to you that I happen to disagree. Sure, it's easy enough to make unreadable code, but it's pretty much impossible to make spaghetti code without GOTO.

Spaghetti code refers to the habit that some people used to have to jump into loops and subroutines halfway and jump back out at will. This makes the control structure pretty much ununderstable to anyone but maybe the programmer. This is impossible if you only have conditional statements, subroutines, while-like loops and structured exception handling.

Edit: typos
Posted By: Gon_ Re: for statement - 26/01/03 06:06 PM
Sorry I can't agree with that.. Ive done a lot of tests on GOTO and WHILE loops. No matter how you construct the GOTO loop, it is not possible to make it faster than a WHILE loop. Please feel free to post some code and prove me wrong wink
Posted By: Gon_ Re: for statement - 26/01/03 06:12 PM
Quote:
using GOTO for anything I can think of is pretty bad coding.


Now come on, this isn't true. How else will you jump from part of a routine to another? This is what GOTO is for. I use it in events to Jump too $1 and execute the relevant code on $2-. It's got little to do with Looping. I believe GOTO looping was used because mIRC never had a dedicated looping function like WHILE.

Using GOTO jumps wisely can be far better and faster coding than a bunch of IF-ELSE checking. However, Using GOTO jumps for loops when we have a WHILE statement doesn't make much sense.
Posted By: codemastr Re: for statement - 26/01/03 07:03 PM
Well for your example of error trapping, as I said with Sabby's example, switch() would be better, in your example error handling (try, except, finally, throw) would be a much better way to do error handling than a goto trap.
Posted By: stygian Re: for statement - 26/01/03 07:14 PM
Quote:

there is nothing a for loop can do what a "var %i = 1 | while (%i <= 10) { bla | inc %i }" can't do... just a little more code.


there is nothing a while loop can do what a "var %i = 1 | :l | if (%i <= 10) { bla | inc %i | goto l }" can't do...just a little more code.

I think the main problem presented by for loops is syntax. mIRC doesn't have any multi-argument looping/conditional commands. It would probably require changes to the parser and a new syntax addition.

I'm ok with inc/dec while loops, although I like for better, but the one I'd really like to see is do/while, specifically for the reason mentioned: sockread event. Or perhaps an identifier akin to $sockbr that tells if there is any data left to be read?

Code:
var %t
dowhile ($sockbr) {
  sockread %t 
  ;do stuff...
}


Code:
var %t
while ($sockdata) {
  sockread %t
  ;do stuff..
}


Either of those would be fine. A "dowhile" loop would definitely be more confusing if done that way, but it would keep closer to the "mIRC feel".

-myndzi
Posted By: Hammer Re: for statement - 26/01/03 07:34 PM
Code:
  var %t
  while (1) {
    sockread %t
    if (!$sockbr) break | ; or halt
 
    ;  The rest of the script
  }
  ;  Cleanup
}
Posted By: Hammer Re: for statement - 26/01/03 07:53 PM
Quote:

Well for your example of error trapping, as I said with Sabby's example, switch() would be better, in your example error handling (try, except, finally, throw) would be a much better way to do error handling than a goto trap.


...none of which are currently in mIRC scripting. Furthermore, the error condition is what you need the goto for, not how it's handled. If-elseif-elseif-else (aka switch (basically)) is how you'd decide just what to do about the error, if anything. You can bet that try/catch won't be in mIRC anytime soon; mIRC is not reentrant in any meaningful way, though you can fake it a bit. Seems to me I remember seeing on ERROR goto whatever .. in many high level languages.

I cannot honestly remember a time when I have written (not modified older code) a goto into a script. The ONLY place I can remember using it in the 3 years since while () was added is in on SOCKREAD, and that was probably more out of habit and follow the old example in the help file to get everything started. It's simple enough to put the code you want to jump to into an alias and simply call that alias (which will return or not).

All that being said, I STILL disagree that ANY language element is "bad." There are perhaps "better" ways of doing things, but GOTO is not inherently bad. What you are objecting to is the bad coding practices that its overuse or misuse lead to; THAT I quite agree with. That's like saying SET is bad because we have /writeini, /var, /hadd, etc. or it's bad because you can easily get your variables written over by another scripter's script that uses the same variable names as yours does (how rude!).

If you truly believe GOTO is bad, I strongly suggest that you stay the hell away from assembly language. laugh
Posted By: qwerty Re: for statement - 27/01/03 06:01 AM
I agree. I don't use goto often in mirc but there are cases where goto does the job in the best possible way (at least for me). The most striking example is using it as another way of switch (in absense of the latter):
Code:
var %a = $1
goto %a
:foo | return 1
:bar | return 2
:%a | return Unknown

I've used goto in a couple of other cases too (but not for looping). Another example would be to break out of nested loops. Only goto can do that at once.

As for arguments of the type "goto makes spaghetti code", that's the programmer's fault. If a programmer doesn't realise he's making spaghetti code, he's hopeless. I don't think any language should remove tools that others might find useful in some cases just because some people would use them in a 'bad' way.
Posted By: SubSpace Re: for statement - 27/01/03 09:04 AM
Just that this is the only sane way to make up for things mIRC lacks doesn't make it right smile

Also note that I referred to 'self respecting higher level language', not some script kiddy language in a chat client :tongue:
Posted By: qwerty Re: for statement - 27/01/03 01:20 PM
"Just that this is the only sane way to make up for things mIRC lacks doesn't make it right"

It makes it right enough if Khaled isn't planning on adding a switch (which is possible seeing that he's already very busy in other fields of this one-man project called mIRC).


"GOTO is the statement that is removed from every self respecting higher level language, or should be."

This statement sounded irrational to me all along. Should be? This is a feature. What entitles you to say which feature should or should not be in a language? If it's useless to you don't use it, but if there is even one person who thinks something is useful, you "should not" have anything to do with it.

Anyway, I'm beginning to repeat what Hammer and others said in this thread, so I'll stop :tongue:
Posted By: vcv Re: for statement - 27/01/03 04:34 PM
Nicely put Hammer.

In the end, all while and for loops use GOTOs (jxx op codes) in every language... the only difference is that it is hidden from you.

GOTO should not ever be used by beginners. But for programmers that know what they are doing, I don't see ANYTHING wrong with using goto, as long as they do not abuse it.

But I repeat.. in the end, all our code is eventually translated to goto's.
Posted By: codemastr Re: for statement - 27/01/03 07:00 PM
if (%a == foo) return 1
else if (%a == bar) return 2
else return Unknown

That would be a better way to do it than a goto. More easily readable, you can follow what is going on much more clearly.
Posted By: Gon_ Re: for statement - 28/01/03 04:01 AM
Ofcourse with your example nobody would use GOTO, That would be stupid. Readability isn't important in such a tiny alias, as you will never go back and debug it. For your example, Speed and Readabiity is not an issue. There are cases where GOTO jumps are far better than using a long stream of IF-ELSE though. Nobody suggested that GOTO was better than IF-ELSE in every possible circumstance. To use a GOTO jump in that instance would be unwarranted.

If that example is meant to illustrate that all uses of GOTO are unwarranted, then I have to disagree. If it's meant to bolster SubSpace's stance of "Goto should be removed from every self respecting language" I have to disagree with that too.

Posted By: qwerty Re: for statement - 28/01/03 12:22 PM
Better? If by that you mean "more readable", this is obviously subjective, as I find both ways equally readable, with the goto way looking cleaner to me.

But "better" has many aspects, one of which is speed: the goto way is faster and this is not subjective. This has to do with the fact that the %a variable isn't evaluated over and over with each if statement: it's evaluated only once and then mirc looks for the literal label that matches the content of %a. Considering that this is scripting -not programming-, which means that variable evaluations cost, this way is "smarter", even if the actual speed gain isn't so important in most cases.
Posted By: codemastr Re: for statement - 28/01/03 11:33 PM
Quote:

This has to do with the fact that the %a variable isn't evaluated over and over with each if statement


Hence the fact why switch() would be very useful as well, it is designed to do exactly what you are talking about.
Posted By: Hammer Re: for statement - 29/01/03 12:40 PM
And in fact, the closest approximation of switch/case, currently, would use exactly that: GOTO %switchlabel
Code:

  ;    switch(condition) {
  ;        case 1,2:
  ;                  code here
  ;                  break;
  ;        case 3:
  ;                  code here
  ;                  break;
  ;        case N:
  ;                  code here
  ;                  break;
  ;        else
  ;                  code here
  ;    }                         // End of switch(condition)
 
  if (condition) {
    goto $ifmatch
 
    :1
    :2
    ;  command(s)
    goto EndOf.Switch | ; break
 
    :3
    ;  command(s)
    goto EndOf.Switch | ; break
 
    :N
    ;  command(s)
    ;  Since this is the last section, goto EndOf.Switch is redundant
 
    :EndOf.Switch
  }
  else {
    ;  command(s)
  }

One pass through evaluating the condition and then go directly to the appropriate section of code (which is the benefit of using switch). Switch is a special case of IF-ELSEIF-ELSE where the exact same condition applies to each case, varying only in the value. This is, in fact, a good example of when you probably SHOULD use GOTO over IF-ELSEIF-ELSE, if speed is an issue.
Posted By: KingTomato Re: for statement - 31/01/03 01:56 AM
Fist off, about the for loop..
Quote:

For loops are different, they are used when initialization is needed and the iteration is standard:

for (%i = 0; %i != 4; inc %i)
{
echo %i
}

Untrue, the variable in the for lop can still be altered judt like any other. Teh differece again is there are less lines of code, a preset stop length, and much more legible.

Seconds, the gotos are not all bad, though I do find them a means of being a 'beginners loop' From my visuals and scripts I have seen, people who do not realize what is going on use goto's, where as more experienced tend to stick with while loops.

Not only the for though would be a good idea, but perhaps a switch statement. Rather then have several labels, provide yet again, another cleaner visually satisfying methos to parse out a variables contents..

Just my 2 cents.. laugh
Posted By: Nem3sis Re: for statement - 31/01/03 02:52 PM
i like the foreach token idea can come in useful also a for next loop would be nice in some cases
Code:
for %a = 1 to 255 | echo -at $chr(%a) | next %a

oh and also goto sux cant really see anywhere where it is better to use a goto....
Posted By: StriderGU Re: for statement - 13/02/03 07:09 AM
I would like to have all the loop types codemastr suggested. smile
© mIRC Discussion Forums