FlawlessHappiness 
 
 
		
			Posting Freak 
			
			
			
 
			
	Posts: 3,980 
	Threads: 145 
	Joined: Mar 2012
	
 Reputation: 
171
		
	 | 
	
		
			
How to determine when for-loop is done 
			 
			
				How do I determine when a for-loop is done? 
Or more specific, in what order does a for-loop run? 
Say, I have
 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?
			  
			
			
 
Trying is the first step to success. 
			
		 |  
	 
 | 
 
	| 06-16-2015, 10:47 AM  | 
	
		
	 | 
 
 
	
		
		Romulator 
 
 
		
			Not Tech Support ;-) 
			
			
			
 
			
	Posts: 3,628 
	Threads: 63 
	Joined: Jan 2013
	
 Reputation: 
195
		
	 | 
	
		
			
RE: How to determine when for-loop is done 
			 
			
				This 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:
  
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.
			  
			
			
 
Discord: Romulator#0001
![[Image: 3f6f01a904.png]](https://puu.sh/zOxJg/3f6f01a904.png)  
			
				
(This post was last modified: 06-16-2015, 11:22 AM by Romulator.)
 
				
			 
		 |  
	 
 | 
 
	| 06-16-2015, 11:15 AM  | 
	
		
	 | 
 
 
	
		
		FlawlessHappiness 
 
 
		
			Posting Freak 
			
			
			
 
			
	Posts: 3,980 
	Threads: 145 
	Joined: Mar 2012
	
 Reputation: 
171
		
	 | 
	
		
			
RE: How to determine when for-loop is done 
			 
			
				Right. 
Thank you for the explanation! 
Does AngelScript use "Next" or should I just write write something after the forloop?
			 
			
			
 
Trying is the first step to success. 
			
		 |  
	 
 | 
 
	| 06-16-2015, 11:24 AM  | 
	
		
	 | 
 
 
	
		
		Romulator 
 
 
		
			Not Tech Support ;-) 
			
			
			
 
			
	Posts: 3,628 
	Threads: 63 
	Joined: Jan 2013
	
 Reputation: 
195
		
	 | 
	
		
			
RE: How to determine when for-loop is done 
			 
			
				 (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   
			 
			
			
 
Discord: Romulator#0001
![[Image: 3f6f01a904.png]](https://puu.sh/zOxJg/3f6f01a904.png)  
			
		 |  
	 
 | 
 
	| 06-16-2015, 11:30 AM  | 
	
		
	 | 
 
 
	
		
		FlawlessHappiness 
 
 
		
			Posting Freak 
			
			
			
 
			
	Posts: 3,980 
	Threads: 145 
	Joined: Mar 2012
	
 Reputation: 
171
		
	 | 
	
		
			
RE: How to determine when for-loop is done 
			 
			
				Awesome! Will check if it works now!
			 
			
			
 
Trying is the first step to success. 
			
		 |  
	 
 | 
 
	| 06-16-2015, 11:32 AM  | 
	
		
	 | 
 
 
	
		
		Romulator 
 
 
		
			Not Tech Support ;-) 
			
			
			
 
			
	Posts: 3,628 
	Threads: 63 
	Joined: Jan 2013
	
 Reputation: 
195
		
	 | 
	
		
			
RE: How to determine when for-loop is done 
			 
			
				<delete> 
Browser had a brain fart P:
			 
			
			
 
Discord: Romulator#0001
![[Image: 3f6f01a904.png]](https://puu.sh/zOxJg/3f6f01a904.png)  
			
				
(This post was last modified: 06-16-2015, 11:34 AM by Romulator.)
 
				
			 
		 |  
	 
 | 
 
	| 06-16-2015, 11:34 AM  | 
	
		
	 | 
 
 
	
		
		Mudbill 
 
 
		
			Muderator 
			
			
			
 
			
	Posts: 3,881 
	Threads: 59 
	Joined: Apr 2013
	
 Reputation: 
179
		
	 | 
	
		
			
RE: How to determine when for-loop is done 
			 
			
				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:
 for(int i = 1; i <= 5, i++) {     if(i == 3) continue;     Print("Step " + i); } Print("End"); 
 
 
It produces:
 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:
 
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.
			  
			
			
 
			
				
(This post was last modified: 06-16-2015, 01:54 PM by Mudbill.)
 
				
			 
		 |  
	 
 | 
 
	| 06-16-2015, 01:45 PM  | 
	
		
	 | 
 
 
	
		
		FlawlessHappiness 
 
 
		
			Posting Freak 
			
			
			
 
			
	Posts: 3,980 
	Threads: 145 
	Joined: Mar 2012
	
 Reputation: 
171
		
	 | 
	
		
			
RE: How to determine when for-loop is done 
			 
			
				Interesting. 
 
'continue' skips what comes after. 
'break' does the same as return, but instead of return something it just stops the loop.
			 
			
			
 
Trying is the first step to success. 
			
		 |  
	 
 | 
 
	| 06-16-2015, 01:51 PM  | 
	
		
	 | 
 
 
	
		
		Mudbill 
 
 
		
			Muderator 
			
			
			
 
			
	Posts: 3,881 
	Threads: 59 
	Joined: Apr 2013
	
 Reputation: 
179
		
	 | 
	
		
			
RE: How to determine when for-loop is done 
			 
			
				Yup. 
 
Break only jumps out of the current loop, like a for-loop or while-loop.
			 
			
			
 
			
		 |  
	 
 | 
 
	| 06-16-2015, 01:53 PM  | 
	
		
	 | 
 
 
	
		
		TheGreatCthulhu 
 
 
		
			Member 
			
			
			
 
			
	Posts: 213 
	Threads: 10 
	Joined: Oct 2010
	
 Reputation: 
32
		
	 | 
	
		
			
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).
			
			
			
			
		 |  
	 
 | 
 
	| 06-16-2015, 02:09 PM  | 
	
		
	 | 
 
 
	 
 |