Thread: Teaching: for()
View Single Post
  #4  
Old 09-08-2003, 02:48 AM
CheeToS2 CheeToS2 is offline
That Guy
CheeToS2's Avatar
Join Date: Dec 2001
Location: Seattle, WA
Posts: 2,528
CheeToS2 will become famous soon enough
Send a message via AIM to CheeToS2
>.>

format:
for (initialization-operation;test;increment-operation){
commands;
}
(don't kill me if I got the words wrong, they still make sense >:O)

initialization-operation = what happens right before the loop begins for the first time.

test = what the loop will check and make sure is true before going onto the next loop.

increment-operation = what happens after one loop completes

EXAMPLE:
for (i=0;i<10;i++){
setplayerprop #c,I CAN COUNT: #v(i)!;
}

OUTPUT:
I CAN COUNT: 0!
I CAN COUNT: 1!
I CAN COUNT: 2!
I CAN COUNT: 3!
I CAN COUNT: 4!
I CAN COUNT: 5!
I CAN COUNT: 6!
I CAN COUNT: 7!
I CAN COUNT: 8!
I CAN COUNT: 9!

WHAT IT MEANS:
for (i=0;i<10;i++){
setplayerprop #c,I CAN COUNT: #v(i)!;
}
i=0 makes it set the variable "i" to 0 before beginning the loop.

for (i=0;i<10;i++){
setplayerprop #c,I CAN COUNT: #v(i)!;
}
i<10 checks whether or not the variable "i" is less than 10 before beginning the loop

for (i=0;i<10;i++){
setplayerprop #c,I CAN COUNT: #v(i)!;
}
i++ makes the variable "i" increase by 1 after a loop completes.


ORDER OF OPERATIONS:
i is set to 0
checks whether or not i is less than 10
if i is less than 10, sets players' chat text to "I CAN COUNT: <value of i>!
increases i by 1
checks whether or not i is less than 10
if i is less than 10, sets players' chat text to "I CAN COUNT: <value of i>!
...and so on



Some people may be confused why it never outputs "I CAN COUNT: 10!".. thats because the test checks if its less than 10, meaning it will not actually include 10. If you wanted to include 10, you could make the test "i<11" or "i<=10"
__________________


Last edited by CheeToS2; 09-08-2003 at 04:20 AM..
Reply With Quote