Poll

Which is better to use?

While
1 (20%)
Repeat / Until
4 (80%)

Total Members Voted: 5

Author Topic: While or Repeat/Until  (Read 6200 times)

0 Members and 1 Guest are viewing this topic.

Offline CervezaTopic starter

  • Hacksimus Maximus
  • Scripthack
  • *
  • Posts: 5857
  • Activity:
    0%
  • Reputation Power: 80
  • Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!
  • Gender: Male
  • So... Hows that Hopey-Changey thing working out?
  • Respect: +403
  • Referrals: 11
    • View Profile
While or Repeat/Until
« on: March 14, 2009, 03:16:22 PM »
0
Here's two snips that do the same thing...

Code: [Select]
while #findCnt <> 0
  findItem !target

Code: [Select]
repeat
  finditem !target
until #findCnt = 0

Both are waiting until a target is killed. I have a timer added to each, but it's unimportant for this question.

OK you real programmers out there... which is better to use, and why?
XXXXXXXXXX________________________________________] 20%
I've forgotten more about this game then most people will ever know.
Thank you for controlling your children. Their manners reflect your love for them.
Give a man a fish and you feed him for a day. Don't teach a man to fish, and you feed yourself. He's a grown man. Fishing's not that hard.

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: While or Repeat/Until
« Reply #1 on: March 14, 2009, 04:04:14 PM »
0
You need to use the repeat/until.  The problem with while is that you have to "prime" the #findCnt to get a valid number, otherwise you'll have random results from the last known result from a finditem command.  That's always why I used an if/goto in scenarios like that because

Code: [Select]
findItem !target
while #findCnt <> 0
  findItem !target

bugs the crap out of me.
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline CervezaTopic starter

  • Hacksimus Maximus
  • Scripthack
  • *
  • Posts: 5857
  • Activity:
    0%
  • Reputation Power: 80
  • Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!
  • Gender: Male
  • So... Hows that Hopey-Changey thing working out?
  • Respect: +403
  • Referrals: 11
    • View Profile
Re: While or Repeat/Until
« Reply #2 on: March 14, 2009, 04:42:20 PM »
0
But, but, but... it's one line shorter  :P

The main difference, is that the "trigger" for the While must be called before the While line. With a repeat/until you can call it between them.

That is if I'm understanding it right.
XXXXXXXXXX________________________________________] 20%
I've forgotten more about this game then most people will ever know.
Thank you for controlling your children. Their manners reflect your love for them.
Give a man a fish and you feed him for a day. Don't teach a man to fish, and you feed yourself. He's a grown man. Fishing's not that hard.

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: While or Repeat/Until
« Reply #3 on: March 14, 2009, 04:45:21 PM »
0
One line shorter doesn't beat unexpected functionality and buggy code.... ;) 
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: While or Repeat/Until
« Reply #4 on: March 14, 2009, 04:50:07 PM »
0
Here, lemme break your code:

Suppose you have some gold in your pack, or you *think* you do:

Code: [Select]
finditem POF C_ , #BACKPACKID
... do something, don't care what
... however, this time you didn't find any gold.....then you run:
while #findCnt <> 0
  findItem !target ; Hmm, you didn't run this loop...

See how this can be buggy depending on what the previous value of #FINDCNT was?
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline rana70

  • Elite
  • *
  • *
  • Posts: 294
  • Activity:
    0%
  • Reputation Power: 5
  • rana70 has no influence.
  • Gender: Male
  • Respect: +37
  • Referrals: 2
    • View Profile
    • MyScripts
Re: While or Repeat/Until
« Reply #5 on: March 14, 2009, 06:53:31 PM »
0
and don't forget the ...
{
}

with the repeat / Until


 ;) 8)

sometimes this might be considered a BUG :'(

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: While or Repeat/Until
« Reply #6 on: March 14, 2009, 06:54:13 PM »
0
and don't forget the ...
{
}

with the repeat / Until


 ;) 8)

sometimes this might be considered a BUG :'(

ROFL!!  I've got your back, Rana.. ;)
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline Endless Night

  • Global Moderator
  • *
  • *
  • Posts: 5467
  • Activity:
    0%
  • Reputation Power: 62
  • Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!
  • Respect: +393
  • Referrals: 1
    • View Profile
Re: While or Repeat/Until
« Reply #7 on: March 16, 2009, 05:24:33 AM »
0
what Repeat Until Rules....    You never need { or } with repeat until

Repeat
   Msg Repeat Until Rules
Until The_Disbelivers_Believe
Outlaw Josey Wales - "Manwink, A Long Gone Scripty, and Endless are always teasing us with their private sections lol. What there realy saying is scripters rule and users drool."
Briza - "Your a living breathing vortex of usefulness."

Offline CervezaTopic starter

  • Hacksimus Maximus
  • Scripthack
  • *
  • Posts: 5857
  • Activity:
    0%
  • Reputation Power: 80
  • Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!
  • Gender: Male
  • So... Hows that Hopey-Changey thing working out?
  • Respect: +403
  • Referrals: 11
    • View Profile
Re: While or Repeat/Until
« Reply #8 on: March 16, 2009, 05:33:59 AM »
0
Quote
what Repeat Until Rules....    You never need { or } with repeat until

Repeat
   Msg Repeat Until Rules
Until The_Disbelivers_Believe

You need them if there's more then one line. I know you can use while without {} also, as long as the following code is just one line long. Anything longer you need to use {}.

If you just want to check for something that is a one liner, while sure works great.

Code: [Select]
set #targCurs 1
while #tarcCurs = 1
  wait 0

Code: [Select]
set #targCurs 1
repeat
{
  wait 0
  until #targCurs <> 1
}

FindItem or something that is a one line thing, or even a counter or clock would work fine with a while. A repeat has to execute the code inside it to determine it's condition, a while can use whatever is currently in the script.

Code: [Select]
if C in #charStatus
{
  wait 10
  gosub cure
}
.....
sub cure
while C in #charStatus
{
cast arch cure, drink cure pots, cleanse by fire
}
return

The gosub is called if the char gets poisoned, but While will only function if the char is still poisoned. It's like an extra check to see if the char is poisoned.

Code: [Select]
if C in #charStatus
{
  wait 10
  gosub cure
}
.....
sub cure
repeat
{
cast arch cure, drink cure pots, cleanse by fire
}
until C notin #charStatus
return


Again, the sub is called if the char is poisoned. Then it will start casting and drinking without verifying the char is still poisoned or not. It must do it at least one time.

In this case you *might* not need to go through the cure process of the sub. Lets say you get poisoned, #charStatus goes to C and the script goes to execute the sub, perhaps you were only poisoned for an instant, like when you are in Vamp Embrace. While won't even execute it's portion because #charStatus is no longer C. Repeat will start casting or drinking regardless.

To make Repeat safe, you would actually have to check #charStatus AGAIN inside it to save it from casting, where While wouldn't need that check.

I agree that while *could* cause problems in poorly structured scripts. Same as ANY misused command.
« Last Edit: March 16, 2009, 05:39:55 AM by Cerveza »
XXXXXXXXXX________________________________________] 20%
I've forgotten more about this game then most people will ever know.
Thank you for controlling your children. Their manners reflect your love for them.
Give a man a fish and you feed him for a day. Don't teach a man to fish, and you feed yourself. He's a grown man. Fishing's not that hard.

Offline Endless Night

  • Global Moderator
  • *
  • *
  • Posts: 5467
  • Activity:
    0%
  • Reputation Power: 62
  • Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!
  • Respect: +393
  • Referrals: 1
    • View Profile
Re: While or Repeat/Until
« Reply #9 on: March 16, 2009, 05:58:50 AM »
0
You need them if there's more then one line. I know you can use while without {} also, as long as the following code is just one line long. Anything longer you need to use {}.

If you just want to check for something that is a one liner, while sure works great.


Where is this Myth coming from about the { } .. I know ive been off for a while... but i Have never used and {} in Repeat loops ever.  And yes most are well over one line and many are embedded...

 Repeat
   blab
   bla
  repeat
    bla
    bla
  until x
 until y


But while are also useful .. it depends how you code .. i use repeat a factor of 20 more than whiles... but occasionally ill use a while.  Gotos thow never.

Outlaw Josey Wales - "Manwink, A Long Gone Scripty, and Endless are always teasing us with their private sections lol. What there realy saying is scripters rule and users drool."
Briza - "Your a living breathing vortex of usefulness."

Offline CervezaTopic starter

  • Hacksimus Maximus
  • Scripthack
  • *
  • Posts: 5857
  • Activity:
    0%
  • Reputation Power: 80
  • Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!Cerveza is awe-inspiring!
  • Gender: Male
  • So... Hows that Hopey-Changey thing working out?
  • Respect: +403
  • Referrals: 11
    • View Profile
Re: While or Repeat/Until
« Reply #10 on: March 16, 2009, 06:42:41 AM »
0
QFT - I had forgotten about the Repeats default closing bracket of Until. I just got in the habit of enclosing anything more then one line in {} and I still do it with repeats.
XXXXXXXXXX________________________________________] 20%
I've forgotten more about this game then most people will ever know.
Thank you for controlling your children. Their manners reflect your love for them.
Give a man a fish and you feed him for a day. Don't teach a man to fish, and you feed yourself. He's a grown man. Fishing's not that hard.

Offline 12TimesOver

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: While or Repeat/Until
« Reply #11 on: March 16, 2009, 07:47:33 AM »
0
I don't think one is better than the other in general, I think they both have their places. I use REPEAT when I need a loop to run once regardless of criteria being met in advance whereas I tend to use WHILE when the criteria has been met before the loop. Pretty simple I guess, there is a use for both and either can be used exclusively if that is how you feel like coding.

I get super-annoyed with having to pre-load a variable on purpose just to utilize WHILE but, on the other hand, I'm basically doing the same thing within the REPEAT loop by having the criteria checked AFTER the action.

Oh, and I like REPEAT for my _mainloop's:

Code: [Select]
REPEAT
   Gosub 1
   Gosub 2
   Gosub 3
UNTIL #FALSE

Probably not the most wonderful use but it works for me :p

Funny Cerv and EN, it never even occured to me that the UNTIL was an inferred "close bracket" so I looked back through some of my code and found examples where I've done it both ways though LOL. Guess using brackets isn't such a bad habit to get into really.

XII
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline _C2_

  • AFK FtW
  • Global Moderator
  • *
  • *
  • Posts: 4077
  • Activity:
    0%
  • Reputation Power: 48
  • _C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!_C2_ has great potential!
  • RIP Pen Trick
  • Respect: +254
  • Referrals: 4
    • View Profile
Re: While or Repeat/Until
« Reply #12 on: March 16, 2009, 07:54:02 AM »
0
I do not user {} for repeat ever... :o

Offline rana70

  • Elite
  • *
  • *
  • Posts: 294
  • Activity:
    0%
  • Reputation Power: 5
  • rana70 has no influence.
  • Gender: Male
  • Respect: +37
  • Referrals: 2
    • View Profile
    • MyScripts
Re: While or Repeat/Until
« Reply #13 on: March 16, 2009, 09:07:29 AM »
0
I do not user {} for repeat ever... :o

Dont't worry about it .. it works great without them ..
but this is part of the "offical" dokumentation to use them ...
it was just a kind of an insider joke .. TM knew what I was talking about....

cu
 

Offline Endless Night

  • Global Moderator
  • *
  • *
  • Posts: 5467
  • Activity:
    0%
  • Reputation Power: 62
  • Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!Endless Night is awe-inspiring!
  • Respect: +393
  • Referrals: 1
    • View Profile
Re: While or Repeat/Until
« Reply #14 on: March 16, 2009, 01:26:19 PM »
0
Well the docs are a wikki right .. so who knows someone was having some fun lol.
Outlaw Josey Wales - "Manwink, A Long Gone Scripty, and Endless are always teasing us with their private sections lol. What there realy saying is scripters rule and users drool."
Briza - "Your a living breathing vortex of usefulness."

Tags: