« Changelog 11/10/2009 | New to ArmA 2: Threads » |
New to ArmA 2: elseif
elseif? We already have else if!
More or less. We have else { if ... } which is different. Let's look at an example:
Code:
if (Area1Clear) then { | |
Convoi_Next_Waypoint = Area1; | |
} else { | |
if (Area2Clear) then { | |
Convoi_Next_Waypoint = Area2; | |
} else { | |
if (Area3Clear) then { | |
Convoi_Next_Waypoint = Area3; | |
} else { | |
Convoi_Next_Waypoint = objNull; | |
//call for reinforcement... | |
}; | |
}; | |
}; |
This is getting ugly as soon as we require more checks.
...
But thanks to a note from Iva we have a better solution:
Code:
switch (true) do { | |
case (Area1Clear): { | |
Convoi_Next_Waypoint = Area1; | |
}; | |
case (Area2Clear): { | |
Convoi_Next_Waypoint = Area2; | |
}; | |
case (Area3Clear): { | |
Convoi_Next_Waypoint = Area3; | |
}; | |
default { | |
Convoi_Next_Waypoint = objNull; | |
//call for reinforcement... | |
}; | |
}; |
Nearly a normal switch-block, only the condition is true and our conditions are put in the case.
How is this an elseif?
The switch will start checking the conditions from the top. So Area1Clear will be the first checked, then Area2Clear and so on. If one condition is true, the related is going to get executed. But instead of continuing to check the conditions, the switch statement will be left. A forced break for those who know C(++).
We finally got an easy structure which replaces an elseif without a lot of braces.
Ok, it's not an elseif, but as good as
1 comment
