Frictional Games Forum (read-only)

Full Version: How to determine when for-loop is done
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How do I determine when a for-loop is done?

Or more specific, in what order does a for-loop run?
Say, I have

PHP Code:
void Function()
{
for(
int i=1;i<=5;i++)
{
if(
GetLocalVarInt("Name_"+i) == 1)
{
return 
int(i);
}
}

SetEntityActive("Thing"false);


When this runs, it goes through 1 to 5, and if one of the local var named "Name_1" to "Name_5" are 1, it'll jump out of the function.
But when is SetEntityActive called? After the for-loop has run through all the numbers?
This code:
Code:
Module VBModule
Dim intInteger as Integer = 0
    Sub Main()
        For intInteger = 0 To 4
            Console.Writeline("The current integer is: " & intInteger)
        Next
        Console.Writeline("This is outside the For-Next Loop")
    End Sub
  
End Module

Will produce this:
[Image: ca252a5fce.png]

So it will do the For-Next loop first, then after it has finished, will run the proceeding code.
While that is Vb.Net, since code is sequential, that is, every thing occurs in the order it is fed sequentially, a computer will follow instructions in the order it has been written. A For-Next Loop will repeat until the loop condition is met, then carry out the following code.

This is why when you make a While loop and cause an infinity loop, the game crashes. Because it cannot end it, uses up resources trying to and fails to execute any latter code before hanging.
Right.
Thank you for the explanation!
Does AngelScript use "Next" or should I just write write something after the forloop?
(06-16-2015, 11:24 AM)FlawlessHappiness Wrote: [ -> ]Right.
Thank you for the explanation!
Does AngelScript use "Next" or should I just write write something after the forloop?

AngelScript doesn't use "Next". "Next" is just there so VB.Net knows when to repeat the loop. AngelScript just uses the braces to determine where to start looping - as your code above does Smile
Awesome! Will check if it works now!
<delete>
Browser had a brain fart P:
Although these might not exactly answer your question individually, they're useful to know. I haven't tested them all in AngelScript, but I assume they work.

In Java, there's a keyword called "continue" which will skip the remaining code within a loop, but then continue the loop after. So for example you could do:

PHP Code:
for(int i 1<= 5i++) {
    if(
== 3) continue;
    Print(
"Step " i);
}
Print(
"End"); 

It produces:

Code:
Step 1
Step 2
Step 4
Step 5
End

There's another keyword called "break" which is similar, but it also stops the remaining iterations of the loop. Let's use the above example, but have "break" instead of "continue." It will produce this:

Code:
Step 1
Step 2
End

You already know of "return" so I won't go there. But just for reference, using it would kill the "End" as well.

Another keyword is "goto" but it should not be used, at least in Java. Perhaps it's usable in C.
Interesting.

'continue' skips what comes after.
'break' does the same as return, but instead of return something it just stops the loop.
Yup.

Break only jumps out of the current loop, like a for-loop or while-loop.
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).
Pages: 1 2