Author Topic: Repeat Until While  (Read 12052 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
Repeat Until While
« on: April 18, 2011, 05:35:27 AM »
+1
Ok, lets get into this.

I mentioned in another tutorial that I'd talk about Repeat/Until and While. And he it is.

Both will do whatever follows over and over until a condition is met.

Lemme show you an example of both.

Code: [Select]
repeat
check the amount of beer in Cerv's glass
until the amount of beer in Cerv's glass = 0

See how whatever the until line is checking for has to be done in between the repeat-until? Now to do that with a while statement we have to check first, then check again inside.... watch

Code: [Select]
check the amount of beerin Cerv's glass
while the amount of beer <> 0
{
Cerv chills out and drinks beer
check the amount of beerin Cerv's glass
}
See how the while statement needed to check the glass both before and during the statement to see if it met criteria?

In that case, using Repeat/Until makes much more sense.

Here's Cerv's Rules For Using Repeat/Until or While:

If I'm checking something that I have set in the script - use Repeat/Until.
If I'm checking for something that EUOx sets - use While.

What that means is like in the example above. I'm checking for something that I have setup in the script. So I would use the Repeat/Until.

Now here's when I like to use While.

Code: [Select]
repeat
do this thing that requires mana
until #mana < 10

See that will repeat whatever is in the middle until my mana drops below 10. Since #mana is an EUOx function thingy, I like to use While for this.

Code: [Select]
while #mana > 10
do this thing that requires mana

You can see the difference right there. If it's a # command from EUOx I always prefer to use While because I don't have to constantly check whatever is the criteria in the statement.

Another thing to remember when using repeat/until and while.

Repeat/Until will do everything in between them. Everything.

Code: [Select]
repeat
do this
do that
do the other
do some more
until #done

With While it will only do the NEXT LINE OF CODE unless you use brackets {}

Code: [Select]
while something is something
do this
do that
do the other
do some more

That code will ONLY do the one line under the while (do this) until the criteria is met (something is something). The correct usage would be:

Code: [Select]
while something is something
{
do this
do that
do the other
do some more
}

By putting everything in brackets they will all be done until the criteria is met.

That's about it. Hopefully it'll spark some conversation.

Key Points:

1 - Use Cerv's Rules, use repeat/until if I set it, use while if EUOx sets it.
2 - Repeat does everything between it and Until, While does the next line or between the brackets {}
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: Repeat Until While
« Reply #1 on: April 18, 2011, 06:16:13 AM »
+2
This is how I differentiate/think of repeat/until or while

- While: Check before doing X
- Repeat/Until: Do X then check before doing X Again.

So WHILE might never do X .. where as Repeat/Until is Guaranteed to do X at least once  
(where X = a line or block of code)


Which is why Repeat/Until makes a great loop for the main function of your script.  Eg
Code: [Select]
; script start
repeat
  gosub scriptaction1
  gosub scriptaction2
  gosub scriptaction3  ; etc
until %ScriptFinished
halt  ; script end
; subs below

EDIT: Nice write up Cerveza


« Last Edit: April 18, 2011, 06:53:51 AM by Endless Night »
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 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: Repeat Until While
« Reply #2 on: April 18, 2011, 06:47:23 AM »
+1
Great write-up Cerv, can always use more script tutorials and I'm soooo damn lazy lately...

Exactly how I think about it EN. Anytime I need something run until a criteria is met the choice is REPEAT/UNTIL, if I need something run only if a criteria is met it's a WHILE.

I always use REPEAT/UNTIL as my main loop nowadays as well.

Code: [Select]
repeat
   gosub sub1
   gosub sub2
   bosub subetc
until #FALSE ;<--- which will never evaluate as true thus loops 4-evah!

X
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 UOMaddog

  • Maddog
  • Elite
  • *
  • *
  • Posts: 1625
  • Activity:
    0%
  • Reputation Power: 22
  • UOMaddog might someday be someone...UOMaddog might someday be someone...UOMaddog might someday be someone...UOMaddog might someday be someone...
  • Gender: Male
  • Biggest B@D@$$ of the Universe
  • Respect: +165
  • Referrals: 8
    • View Profile
    • Insane UO
Re: Repeat Until While
« Reply #3 on: April 18, 2011, 07:15:18 AM »
+1
I agree 100% with EN's outlook on it.

Repeat/Until (essentially do/while loops in C) guarantee the code executes once! This is perfect for main loops, since it's assumed you want the script to run at least once (and perhaps more).

While loops will only execute IF the condition is already met, so they're great for "waits" in my opinion. Such as:
Code: [Select]
set #targcurs 1
while #targcurs <> 1
   wait 1
Using this method, you set the cursor, and ideally check the next WHILE condition (which hopefully fails) and you move along (no waiting at all!) If you had used a repeat/until instead, you'd have to do:
Code: [Select]
repeat
set #targcurs 1
wait 1
until #targcurs = 1
Note the added wait 1, which is now guaranteed.
Now you could do:
Code: [Select]
set #targcus 1
repeat
wait 1
until #targcurs=1
which would execute the same, but now you've used 3 lines of code (and still guaranteed a wait 1) when you could have used 2 lines (and not necessarily any waiting)
There are 10 kinds of people in this world: those that understand binary and those that don't!

Windows:  A 64-bit tweak of a 32-bit extension to a 16-bit user interface for an 8-bit operating system based on a 4-bit architecture from a 2-bit company that can't stand 1 bit of competition!

Offline gimlet

  • Very Super Secret
  • Global Moderator
  • *
  • *
  • Posts: 6191
  • Activity:
    3%
  • Reputation Power: 71
  • gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!
  • Gender: Male
  • Respect: +273
  • Referrals: 3
    • View Profile
Re: Repeat Until While
« Reply #4 on: April 18, 2011, 07:44:54 AM »
+1
These are great - how about a lua or open example

Offline Tidus

  • Lazy
  • Administrator
  • *
  • *
  • Posts: 1291
  • Activity:
    0%
  • Reputation Power: 15
  • Tidus is working their way up.Tidus is working their way up.Tidus is working their way up.
  • Gender: Male
  • Mind Blown
  • Respect: +151
  • Referrals: 2
    • View Profile
    • Ultimate Apparel
Re: Repeat Until While
« Reply #5 on: April 18, 2011, 02:36:40 PM »
+1
you should throw a For Loop in there and just get all the loops out of the way ;)
For those who have fought for it, freedom has a taste the protected will never know ~ Anonymous, Vietnam, 1968

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: Repeat Until While
« Reply #6 on: September 20, 2011, 06:40:10 AM »
+1
I still go with using while when the "trigger" is something with a # in front of it LOL.

If I need to wait for a condition to be met, or check a condition over and over, and that condition is a set EUO function, then I use while because I don't have to constantly check it with a repeat/until

while #mana < 20
  wait 1

Simple 2 line to wait for mana to be greater then 20. When doing the same thing with repeat/until

repeat
  wait 1
until #mana > 20

Additional line that's not needed.

Also, the repeat/until is actually a loop, correct? I'd be interested to see if there's CPU difference between those two examples...
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 UOMaddog

  • Maddog
  • Elite
  • *
  • *
  • Posts: 1625
  • Activity:
    0%
  • Reputation Power: 22
  • UOMaddog might someday be someone...UOMaddog might someday be someone...UOMaddog might someday be someone...UOMaddog might someday be someone...
  • Gender: Male
  • Biggest B@D@$$ of the Universe
  • Respect: +165
  • Referrals: 8
    • View Profile
    • Insane UO
Re: Repeat Until While
« Reply #7 on: September 20, 2011, 12:15:19 PM »
+1
Looking at your original example, you have a slight flaw you might want to look at:

You used two examples:

Example 1:
Code: [Select]
repeat
do this thing that requires mana
until #mana < 10
Example 2:
Code: [Select]
while #mana > 10
  do this thing that requires mana

The flaw is that in example 1, you never check to see if you even HAVE 10 mana before attempting to execute it. So in this case, example 2 is by far the better one!

As EN stated, the real difference is:

Repeat/Until - Guarantees the action executes AT LEAST ONCE and then checks if it should repeat (Do, then Check)
While - Checks if it should execute THEN does the action (Check, then Do)
There are 10 kinds of people in this world: those that understand binary and those that don't!

Windows:  A 64-bit tweak of a 32-bit extension to a 16-bit user interface for an 8-bit operating system based on a 4-bit architecture from a 2-bit company that can't stand 1 bit of competition!

Offline camotbik

  • Sr. Member
  • *
  • Posts: 349
  • Activity:
    0%
  • Reputation Power: 3
  • camotbik has no influence.
  • Gender: Male
  • Hello! I'm a UO addict.
  • Respect: +38
  • Referrals: 0
    • View Profile
Re: Repeat Until While
« Reply #8 on: September 20, 2011, 01:02:42 PM »
+1
I just wanted to add, that at multiple lines you have to use brackets inside repeat/until circle.
Code: [Select]
Synopsis : repeat { } until ( expression )
http://wiki.easyuo.com/index.php?title=Repeat..until
What you witness -- is whatver..
uogamers hybrid.

Offline Neo

  • Prime Program
  • Elite
  • *
  • *
  • Posts: 821
  • Activity:
    0%
  • Reputation Power: 13
  • Neo barely matters.Neo barely matters.
  • Respect: +155
  • Referrals: 3
    • View Profile
Re: Repeat Until While
« Reply #9 on: September 20, 2011, 01:37:04 PM »
+1
I just wanted to add, that at multiple lines you have to use brackets inside repeat/until circle.
Code: [Select]
Synopsis : repeat { } until ( expression )
http://wiki.easyuo.com/index.php?title=Repeat..until
I've never used brackets inside repeat/until cycles... Never found any issues...
Never refuse an invitation.
Never resist the unfamiliar.
Never fail to be polite.
And never outstay your welcome.

Offline camotbik

  • Sr. Member
  • *
  • Posts: 349
  • Activity:
    0%
  • Reputation Power: 3
  • camotbik has no influence.
  • Gender: Male
  • Hello! I'm a UO addict.
  • Respect: +38
  • Referrals: 0
    • View Profile
Re: Repeat Until While
« Reply #10 on: September 20, 2011, 01:41:50 PM »
+1
well many people don't close they're subs with a final return and don't encounter any problems as well.. But if the syntax says they're should be there, there must be a reason for that.
What you witness -- is whatver..
uogamers hybrid.

Offline gimlet

  • Very Super Secret
  • Global Moderator
  • *
  • *
  • Posts: 6191
  • Activity:
    3%
  • Reputation Power: 71
  • gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!gimlet is awe-inspiring!
  • Gender: Male
  • Respect: +273
  • Referrals: 3
    • View Profile
Re: Repeat Until While
« Reply #11 on: September 20, 2011, 02:40:17 PM »
+1
Whats the open EUO equivalent of these statements?

Offline Neo

  • Prime Program
  • Elite
  • *
  • *
  • Posts: 821
  • Activity:
    0%
  • Reputation Power: 13
  • Neo barely matters.Neo barely matters.
  • Respect: +155
  • Referrals: 3
    • View Profile
Re: Repeat Until While
« Reply #12 on: September 20, 2011, 03:24:00 PM »
+1
Well, if the SYNTAX says it, who am I to question? :)
Never refuse an invitation.
Never resist the unfamiliar.
Never fail to be polite.
And never outstay your welcome.

Offline Neo

  • Prime Program
  • Elite
  • *
  • *
  • Posts: 821
  • Activity:
    0%
  • Reputation Power: 13
  • Neo barely matters.Neo barely matters.
  • Respect: +155
  • Referrals: 3
    • View Profile
Re: Repeat Until While
« Reply #13 on: September 20, 2011, 03:34:01 PM »
+1
Whats the open EUO equivalent of these statements?

In Cerveza's words:

Code: [Select]
while(Mana>=10)do
do this thing that requires mana
end

Code: [Select]
repeat
do this thing that requires mana
until(Mana<=10)

I think that's it...

I could be wrong though...
Never refuse an invitation.
Never resist the unfamiliar.
Never fail to be polite.
And never outstay your welcome.

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: Repeat Until While
« Reply #14 on: September 20, 2011, 04:26:45 PM »
+1
I just wanted to add, that at multiple lines you have to use brackets inside repeat/until circle.
Code: [Select]
Synopsis : repeat { } until ( expression )
http://wiki.easyuo.com/index.php?title=Repeat..until

The above statement is incorrect.

{}  are not required on Repeat..Until   ever.
They are required on While  loops of more than one line of code
They are Required on For loops of more than one line of code

The wiki can be wrong. As its editable by use users.

edit:fixed typo
« Last Edit: September 21, 2011, 04:22:21 AM by Endless Night »
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: