Jump to content

Recommended Posts

Are there any differences between this two?

for "_i" from 1 to 2 do { 
               
   {
     //code...
   } forEach _this;        
        
};
 {
   for "_i" from 1 to 2 do {
      
      //code...
   };
 } forEach _this;
 

If not which one is more "normal" usage?

Share this post


Link to post
Share on other sites

I would say yes, those are basically the same. From a code complexity standpoint, those are definetly the same.

As far as common usage, you get to choose don't you? lol

If your're only going from 1 to 2, there really isn't a need for the for loop, as it will only run once, but for any other number x to y where y minus x > 1, the yea use the for loop.

Share this post


Link to post
Share on other sites

Yes i am always posting stupid questions,

what i am if i am asking my self stupid questions?

Share this post


Link to post
Share on other sites

They aren't stupid questions, as it's easy to not see what you posted as the same thing unless you've had some sort of understanding of code complexity. Easiest thing to do in that situation was to write out what the code said (I may have said all this before, idk...), which is where you'll see that the two basically say the same thing.

Share this post


Link to post
Share on other sites

What if _this is not an array, or, say, empty array?

In first case you will examine it twice, whereas in the second case you will loop on _i only if you have valid iteration on _this' item.

In general, if you have some loop possibly breaking, you better have it earlier (more outer).

Share this post


Link to post
Share on other sites

What if _this is not an array, or, say, empty array?

In first case you will examine it twice, whereas in the second case you will loop on _i only if you have valid iteration on _this' item.

In general, if you have some loop possibly breaking, you better have it earlier (more outer).

If this were my function, and it relied on an array being passed in, I would throw a check on it before ever getting to the loop portion of the code, if there is nothing in the array, there is no reason to run the function.

params [["_arrayIn",[],[[]]]];

private _count = count _arrayIn;
if (_count == 0) exitWith {diag_log "Array entered function with no elements."};

for "_i" from 0 to (_count - 1) do 
{
	{
		hintSilent str(_x);
	} forEach _arrayIn;
};

It's important to write functions that analyze their input data to make sure that it will function properly, and throw errors when necessary so the end user can fix their input errors.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×