while: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Some wiki formatting)
(Moved KK's note about unscheduled 10000 iteration limitation into description)
 
(10 intermediate revisions by 2 users not shown)
Line 25: Line 25:


|descr= Repeats [[Code]] while the given condition is [[true]]. A part of [[while]]-[[do]] construct.
|descr= Repeats [[Code]] while the given condition is [[true]]. A part of [[while]]-[[do]] construct.
 
{{Feature | Important |
* A [[while]] loop does not have any suspension, meaning that if used in [[Scheduler#Scheduled_Environment|scheduled environment]] without any suspension ([[sleep]] or [[uiSleep]]) the code will run multiple times per frame and consumes the 3 ms per frame scheduler execution limit. This should be avoided if not intended (see {{Link|#Example 3}}).
* In [[Scheduler#Unscheduled_Environment|non-scheduled environment]], [[while]] [[do]] loop is limited to 10,000 iterations, after which it exits even if condition is still [[true]].
}}
|s1= [[while]] condition
|s1= [[while]] condition


Line 32: Line 35:
|r1= [[While Type]]
|r1= [[While Type]]


|x1= <code>[[while]] {a < b} [[do]] {a = a + 1};</code>
|x1= <sqf>while { a < b } do { a = a + 1 };</sqf>


|x2= A practical example: Repair all members of a group to such a level that they are able to stand up:
|x2= A practical example: Repair all members of a group to such a level that they are able to stand up:
<code>{
<sqf>
[[if]] ([[alive]] [[Magic Variables#x|_x]]) [[then]]
{
if (alive _x) then
{
{
[[while]] { [[not]] [[canStand]] [[Magic Variables#x|_x]] } [[do]]
while { not canStand _x } do
{
{
[[Magic Variables|_x]] [[setDamage]] ([[damage]] [[Magic Variables|_x]] - 0.01);
_x setDamage (damage _x - 0.01);
};
};
};
};
} [[forEach]] [[units]] [[group]] unitname;</code>
} forEach units group unitname;
</sqf>
 
|x3= <sqf>
[] spawn {
// warning: while loop without suspension executes multiple times per frame
private _counter = 0;
private _endTime = diag_tickTime + 5;
private _frameNo = diag_frameNo;
while { diag_tickTime < _endTime } do
{
_counter = _counter + 1;
};
// in an empty mission, the _counter may go well over 2000 times per frame!
hint format ["Average Execution: %1 times per frame", _counter / (diag_frameNo - _frameNo)];


|seealso= [[Control Structures]], [[waitUntil]], [[for]], [[do]]
// with suspension
private _counter = 0;
private _endTime = diag_tickTime + 5;
private _frameNo = diag_frameNo;
while { diag_tickTime < _endTime } do
{
_counter = _counter + 1;
uiSleep 0.001; // waits at least 1 frame
};
// _counter says one per frame, as expected
hint format ["Average Execution: %1 times per frame", _counter / (diag_frameNo - _frameNo)];
};
</sqf>
 
|seealso= [[Control Structures]] [[waitUntil]] [[for]] [[do]]
}}
}}


Line 52: Line 84:
|timestamp= 20080514084000
|timestamp= 20080514084000
|text= The boolean code that is used to evaluate the while condition can be preceded by code that executes a regular command.
|text= The boolean code that is used to evaluate the while condition can be preceded by code that executes a regular command.
<code>[[while]] {_a =_a + 1; _a < 10} [[do]] {...}</code>
<sqf>while { _a =_a + 1; _a < 10 } do { /* ... */ };</sqf>
}}
 
{{Note
|user= Killzone Kid
|timestamp= 20140830081200
|text= In [[Scheduler#Unscheduled_Environment|non-scheduled environment]], [[while]] [[do]] loop is limited to 10,000 iterations, after which it exits even if condition is still [[true]]. In [[Scheduler#Unscheduled_Environment|scheduled environment]] no such limit exists.
}}
}}

Latest revision as of 08:52, 7 November 2023

Hover & click on the images for description

Description

Description:
Repeats Code while the given condition is true. A part of while-do construct.
  • A while loop does not have any suspension, meaning that if used in scheduled environment without any suspension (sleep or uiSleep) the code will run multiple times per frame and consumes the 3 ms per frame scheduler execution limit. This should be avoided if not intended (see Example 3).
  • In non-scheduled environment, while do loop is limited to 10,000 iterations, after which it exits even if condition is still true.
Groups:
Program Flow

Syntax

Syntax:
while condition
Parameters:
condition: Code
Return Value:
While Type

Examples

Example 1:
while { a < b } do { a = a + 1 };
Example 2:
A practical example: Repair all members of a group to such a level that they are able to stand up:
{ if (alive _x) then { while { not canStand _x } do { _x setDamage (damage _x - 0.01); }; }; } forEach units group unitname;
Example 3:
[] spawn { // warning: while loop without suspension executes multiple times per frame private _counter = 0; private _endTime = diag_tickTime + 5; private _frameNo = diag_frameNo; while { diag_tickTime < _endTime } do { _counter = _counter + 1; }; // in an empty mission, the _counter may go well over 2000 times per frame! hint format ["Average Execution: %1 times per frame", _counter / (diag_frameNo - _frameNo)]; // with suspension private _counter = 0; private _endTime = diag_tickTime + 5; private _frameNo = diag_frameNo; while { diag_tickTime < _endTime } do { _counter = _counter + 1; uiSleep 0.001; // waits at least 1 frame }; // _counter says one per frame, as expected hint format ["Average Execution: %1 times per frame", _counter / (diag_frameNo - _frameNo)]; };

Additional Information

See also:
Control Structures waitUntil for do

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Kronzky - c
Posted on May 14, 2008 - 08:40 (UTC)
The boolean code that is used to evaluate the while condition can be preceded by code that executes a regular command.
while { _a =_a + 1; _a < 10 } do { /* ... */ };