7/22/08

V: Looping

Introduction


Looping is doing a group of functions repeatedly until certain declared condition is achieved. This is useful in WoW macros to do functions that can be repeatedly done without delay.

The reason is, functions done in loop(or any other macro) are done in a very microscopic time distances. In other words every commands/functions are executed one by one but too fast to have anything to be done in between them, almost no time gap.

Three Kind of loops



for do end

/run for (variable name)=(starting value),(end value) do (block of codes) end

while do end

/run (variable name)=(starting value) while (condition) do (block of codes) end

repeat until

/run repeat (block of codes) until (condition)

for do end loop examples



Counting to 3
/run for x=1,3 do SendChatMessage(x) end


when loop executes x will be equal to the starting number which is 1
then it will send the chat message to say the value of variable x which is 1 so it will say in chat: 1
then it will increase x by one so now, value of x is 2
then it will send the chat message to say the value of variable x which is 1 so it will say in chat: 2
then it will increase x by one so now, value of x is 3
then it will send the chat message to say the value of variable x which is 1 so it will say in chat: 3

After that x reached the value of the end value, it will not return to the loop again.
After the loop is over, variable used for the loop will be nil
so you cannot use x anymore unless you declare a new value

While do end Loop Examples:


/run x=1 while x<=3 then SendChatMessage(x) x=x+1 end simulation:
  1. variable x will be equal to 1
  2. if variable x is less than or equal to 3, do if not, go proceed to end(step 6)
  3. SendChatMessage(x) ==> say: 1
  4. variable x = variable x(which is equal to 1) +1 so (x=1+1) so x=2
  5. repeat step 2
  6. end
Note: you can still add command blocks after the "end"
Advantage of using While Loop:
While loops can be added with conditions.
Counter used in while loops doesn't become nil after the loop


repeat until loop example



/run x=1 repeat SendChatMessage(x) x=x+1 until x>=4


Unlike other loop methods, this one doesn't execute if the condition is true, instead, if the condition is false. In the example above, the loop will not break until x is greater or equal to 4
  1. x is equal to 1
  2. SendChatMessage(1) ==> say: 1
  3. x=x+1; x=1+1 ; x=2
  4. check condition: is x>=4? ; False, continue loop (repeat step 2)
  5. SendChatMessage(2) ==> say: 2
  6. x=x+1;x=2+1; x=3
  7. check condition: is x>=4? ; False, continue loop (repeat step 2)
  8. SendChatMessage(3) ==> say:3
  9. x=x+1;x=3+1; x=4
  10. check condition: is x>=4? ; True: 4=4, break the loop, continue to next block(none)

No comments: