Page MenuHomeFeedback Tracker

For-loops misbehave if the last code block is empty.
Closed, ResolvedPublic

Description

If the last code block in the array passed to the 'for' operator is empty, the for-loop will run once, regardless of the condition. Exactly 1 iteration.

This works:
_number = 0;
for [{_i = 0}, {false}, {_i = _i + 1}] do
{

_number = _number + 1;

};

hint str _number;

Expected behaviour: Runs 0 times.
Actual behaviour: Runs 0 times.
Expected output: 0
Actual output: 0

However, if you empty the last code block it will run once.
_number = 0;
for [{_i = 0}, {false}, {}] do
{

_number = _number + 1;

};

hint str _number;

Expected behaviour: Runs 0 times.
Actual behaviour: Runs 1 times.
Expected output: 0
Actual output: 1

Another example:
for [{_i = 0}, {_i < 10}, {}] do
{

createVehicle ["B_MH9_F", getPos player, [], 0, "NONE"];
_i = _i + 1;

};

Expected behaviour: Spawns 10 MH9 helicopters.
Actual behaviour: Spawns 1 MH9 helicopter.

Yet another example:
for [{}, {true}, {}] do
{

hint str diag_tickTime;
sleep 0.1;

};

Expected behaviour: Infinite loop.
Actual behaviour: Runs once.

Details

Legacy ID
3532965363
Severity
None
Resolution
Open
Reproducibility
Always
Category
Scripting
Additional Information

Work around is to put anything in the empty block. eg nil.

for [{}, {true}, {nil}] do
{

hint str diag_tickTime;
sleep 0.1;

};

Event Timeline

MulleDK19 edited Additional Information. (Show Details)
MulleDK19 set Category to Scripting.
MulleDK19 set Reproducibility to Always.
MulleDK19 set Severity to None.
MulleDK19 set Resolution to Open.
MulleDK19 set Legacy ID to 3532965363.May 7 2016, 2:06 PM
MadDogX added a subscriber: MadDogX.May 7 2016, 2:06 PM

Mass closing ancient tickets with no activity for > 12 months; assume fixed or too trivial.

If this issue is still relevant in current dev build, please re-post.