Jump to content
Sign in to follow this  
Doolittle

If then statements eval all??

Recommended Posts

I've noticed ArmA evaluates each argument in an "if" statement.... for example:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if (count _array > 0 and _array select 0 == "foo") then {} ..or..<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if (alive player and name player == "foo") then {}

For the first one, even if your _array has no elements the statement will error out (because it still checks _array select 0). For the second, if the player is dead it still checks to see name of player and this gives error in arma.RPT. SO the script is not like C/C++ in that it doesn't evaluate each argument before moving to next, exiting at first one that isn't true (assuming there's no "or"). Is this a correct understanding???

If that is true then isn't it good practice to split up an "if" statement?? For example:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if ([] call ExpensiveFunc1Code and [] call ExpensiveFunc2Code) then {} would be better written (for efficiency): <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if ([] call ExpensiveFunc1Code) then {

if ([] call ExpensiveFunc2Code) then {}

}

So if ExpensiveFunc1Code is false then ExpensiveFunc2Code won't even be checked. What do you think? I hope I explained this correctly.

Doolittle

Share this post


Link to post
Share on other sites
Quote[/b] ]So if ExpensiveFunc1Code is false then ExpensiveFunc2Code won't even be checked. What do you think? I hope I explained this correctly.

Yeah, I made a similar suggestion here:

http://www.flashpoint1985.com/cgi-bin....t=70000

Right at the bottom of the thread. Just in case you didn't test them, the same applies for config useraction conditions and probably trigger conditions to.

Share this post


Link to post
Share on other sites

This is known as complete vs. short-circuit boolean evaluation. Programming languages like Pascal had complete boolean evaluation, whereas languages like C and C++ have short-circuit evaluation.

As long as the language's behaviour is defined and consistent, you should be OK as you know what to expect from specific statements wink_o.gif

Share this post


Link to post
Share on other sites

Thanks guys. I knew something like that was going on. I didn't know there was a vocab term for it. I'll mention in wiki.

Share this post


Link to post
Share on other sites

Now i'am often use "double if".....:

if ( if (isclass _entry) then {!(configname _entry) in _crash} else {false} ) then {_array=_array+(getclass _entry)}; ...etc

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
Sign in to follow this  

×