Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to determine when for-loop is done
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#10
RE: How to determine when for-loop is done

Yeah, continue and break are part of AngelScript specification, and I think they should work in the game (you can always try). I don't think AngelScript has goto, but even if it did, it should be avoided.

Another important thing to understand about for loops (and loops in general) that people sometimes misunderstand: they don't execute over some period of time while you play - they basically execute between two frames (the whole loop, I mean - all iterations). The game works by giving control to the script at certain points in time; the script should do it's thing and return the control back to the game as fast as possible (this is usually not a problem, all the operations that you guys are used to do in scripts execute really fast).

In any case, as others have said, the loop code will execute over and over again as long as the condition at the top is satisfied, and when it's not, the code will continue with the statements found after the loop.

In your example, if you look at the loop header, there are 3 things in it:
for(int i=1; i<=5; i++)

The first one just starts the i variable to 1.
The second thing is the condition. It says to repeat the loop as long as i is less then or equal to 5.
The third expression is executed at the end of every loop run (when the loop's closing brace } is reached), and it just increments i by 1.

So, basically, in this case the loop is done when i becomes 6.

P.S. About break - it doesn't do quite the same thing as return.

If return is executed within a loop, it will return (exit) from the entire function that contains it.
If break is executed, it will just stop looping and skip to the next line of code under the loop.

P.P.S. When I say "under" the loop, I mean under the closing } (of the loop).
(This post was last modified: 06-16-2015, 02:25 PM by TheGreatCthulhu.)
06-16-2015, 02:09 PM
Find


Messages In This Thread
RE: How to determine when for-loop is done - by TheGreatCthulhu - 06-16-2015, 02:09 PM



Users browsing this thread: 1 Guest(s)