if – Talk
mNo edit summary |
Lou Montana (talk | contribs) m (Text replacement - "\[\[SQS[ _]syntax" to "[[SQS Syntax") |
||
(8 intermediate revisions by 4 users not shown) | |||
Line 42: | Line 42: | ||
:::Sorry, I passed over you have three diferent _var variables. --[[User:Djura|Djura]] 19:29, 24 July 2006 (CEST) | :::Sorry, I passed over you have three diferent _var variables. --[[User:Djura|Djura]] 19:29, 24 July 2006 (CEST) | ||
::::Exactly. We can do fine checking one variable with switch, but as soon as we have different conditions, "else if" is needed (or "elseif", as it's sometimes written) --[[User:Hardrock|hardrock]] 14:48, 25 July 2006 (CEST) | |||
::::: Another way to have "elseif"s is to create a new scope and use exitWith. It looks quite clean and you can even assign a value to a variable this way: | |||
_msg = call { | |||
if (_var == 1) exitWith { | |||
"condition 1"; | |||
}; | |||
if ((_var == 2) && (_var2 == 1)) exitWith { | |||
"condition 2"; | |||
}; | |||
if (_var5 > 0) exitWith { | |||
"condition 3"; | |||
}; | |||
"no condition met"; | |||
}; | |||
hint _msg; | |||
::::: --[[User:Worldeater|Worldeater]] 01:28, 22 February 2009 (CET) | |||
::::: UPDATE: Yet another way (using the ''switch reversed'' trick): | |||
_msg = switch true do { | |||
case (_var == 1) : | |||
{ "condition 1" }; | |||
case ((_var == 2) && ( _var2 == 1)) : | |||
{ "condition 2" }; | |||
case (_var5 > 0) : | |||
{ "condition 3" }; | |||
default | |||
{ "no condition met" }; | |||
}; | |||
::::: --[[User:Worldeater|Worldeater]] 05:22, 22 April 2009 (CEST) | |||
== ? is not alternative syntax == | |||
? is not an alternative syntax for if. ? is a special construct available in [[SQS Syntax]] only, similar to other SQS specific constructs. --[[User:Suma|Suma]] 22:49, 29 September 2007 (CEST) | |||
== Complete Evaluation == | |||
if (count _array > 0 and _array select 0 == "foo") then {} | |||
[[if]] statements are "complete evaluation" rather than "short circuit". In other words everything is evaluated. So the above example would error if _array had no elements because the second part will fail. (Thanks to HitmanFF for pointing out term for this type of evaluation.) --[[User:Doolittle|Doolittle]] 21:19, 28 January 2008 (CET) | |||
:This has actually nothing to do with if. Correct would be to say [[and]] and [[or]] are "complete evaluation" rather than "short circuit". --[[User:Suma|Suma]] 09:23, 29 January 2008 (CET) | |||
Here's a way to fake "Short Circuit": | |||
if (if not isnil "skeetmachine" then {player distance skeetmachine < 30} else {false}) exitwith { ... | |||
--[[User:Doolittle|Doolittle]] 18:09, 29 April 2010 (CEST) |
Latest revision as of 23:01, 24 March 2021
Return value
Will or can the return value of the if statements be changed in such a way, that the normal braces aren't needed anymore to catch the result?
hint if(_var==1) then {"equals 1"} else {"doesn't equal 1"}
doesn't work right now, since OFP returns an error, that you can't pass if types to the hint command. This is not much of a problem, since you can simply put braces arount the if statement.
Where removing this error would be really helpful though, are "else if" statements. Caused by the above circumstances, "else if" statements have to be written like this in OFP:
if (_var==1) then { hint "condition 1"; } else (if (_var==2 && _var2==1) then { hint "condition 2"; } else (if (_var5 > 0) then { hint "condition 3"; } else { hint "no condition filled"; }));
This would look much more beautiful and would be much easier to script, if we could get rid of those braces around if statements, which could be fixed easily (or not) by changing the circumstance pointed out above. --hardrock 13:48, 24 July 2006 (CEST)
- And what about switch?
switch (_var) do { case 1: { hint "condition 1"; }; case 2: { hint "condition 2"; }; default { hint "condition 3"; }; }
- I'm not sure about semicolons, but idea should be correct. --Djura 19:29, 24 July 2006 (CEST)
- Sorry, I passed over you have three diferent _var variables. --Djura 19:29, 24 July 2006 (CEST)
- Exactly. We can do fine checking one variable with switch, but as soon as we have different conditions, "else if" is needed (or "elseif", as it's sometimes written) --hardrock 14:48, 25 July 2006 (CEST)
- Another way to have "elseif"s is to create a new scope and use exitWith. It looks quite clean and you can even assign a value to a variable this way:
_msg = call { if (_var == 1) exitWith { "condition 1"; }; if ((_var == 2) && (_var2 == 1)) exitWith { "condition 2"; }; if (_var5 > 0) exitWith { "condition 3"; }; "no condition met"; }; hint _msg;
- --Worldeater 01:28, 22 February 2009 (CET)
- UPDATE: Yet another way (using the switch reversed trick):
_msg = switch true do { case (_var == 1) : { "condition 1" }; case ((_var == 2) && ( _var2 == 1)) : { "condition 2" }; case (_var5 > 0) : { "condition 3" }; default { "no condition met" }; };
- --Worldeater 05:22, 22 April 2009 (CEST)
? is not alternative syntax
? is not an alternative syntax for if. ? is a special construct available in SQS Syntax only, similar to other SQS specific constructs. --Suma 22:49, 29 September 2007 (CEST)
Complete Evaluation
if (count _array > 0 and _array select 0 == "foo") then {}
if statements are "complete evaluation" rather than "short circuit". In other words everything is evaluated. So the above example would error if _array had no elements because the second part will fail. (Thanks to HitmanFF for pointing out term for this type of evaluation.) --Doolittle 21:19, 28 January 2008 (CET)
- This has actually nothing to do with if. Correct would be to say and and or are "complete evaluation" rather than "short circuit". --Suma 09:23, 29 January 2008 (CET)
Here's a way to fake "Short Circuit":
if (if not isnil "skeetmachine" then {player distance skeetmachine < 30} else {false}) exitwith { ...
--Doolittle 18:09, 29 April 2010 (CEST)