Difference between revisions of "Manual:DIL Manual/while()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = while = | |
| − | + | '''Instruction.''' Repeats a statement as long as a condition is true. | |
| − | |||
| − | |||
| − | + | while (condition) | |
| + | statement | ||
| − | + | Tests <code>condition</code>, and while it is true runs the statement, usually a block in | |
| + | <code>{ }</code>, and tests again. <code>break</code> leaves the loop at once; | ||
| + | <code>continue</code> goes back to the test. | ||
| − | + | == Notes == | |
| − | + | * The condition is true when it is a non-zero integer, a unit or string that isn't null, or a list that isn't empty. A condition that fails (a field of a null unit) is false, so <code>while (u.level > 5)</code> stops when <code>u</code> becomes null. | |
| − | + | * Don't test a text field or a string list entry on its own, as in <code>while (sl.[i])</code> or <code>while (u.title)</code>: for these the test is the wrong way round, true when the text is empty. Write <code>while (sl.[i] != "")</code>. | |
| + | * <code>and</code> and <code>or</code> evaluate both sides: <code>(u != null) and (u.type == UNIT_ST_PC)</code> still reads <code>u.type</code>. That is harmless here, since a failed field is false. | ||
| + | * There is no <code>do ... while</code> and no <code>for</code>. | ||
| + | * A loop that runs about 52000 instructions without a <code>wait</code> or <code>pause</code> gets the program stopped ("had endless loop" in the zone log). A long walk over the world's units needs a <code>pause</code> now and then. | ||
| + | * A loop over a room's units that moves or destroys them should be a [[Manual:DIL Manual/foreach()|foreach()]]: after <code>link(u, ...)</code>, <code>u.next</code> is in the other place. | ||
| − | + | == Example == | |
| − | < | + | <pre> |
| − | + | dilbegin unitptr first_weapon(ch : unitptr); | |
| − | + | var | |
| − | + | u : unitptr; | |
| + | code | ||
| + | { | ||
| + | u := ch.inside; | ||
| + | while (u != null) | ||
| + | { | ||
| + | if (u.type == UNIT_ST_OBJ) | ||
| + | if (u.objecttype == ITEM_WEAPON) | ||
| + | break; | ||
| + | u := u.next; | ||
| + | } | ||
| + | return(u); // null when there is none | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | Finds the first weapon a character carries, or null. | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/break|break]] | |
| − | + | * [[Manual:DIL Manual/continue|continue]] | |
| − | + | * [[Manual:DIL Manual/foreach()|foreach()]] | |
| − | + | * [[Manual:DIL Manual/if()|if()]] | |
| − | + | * [[Manual:DIL Manual/goto|goto]] | |
| − | |||
| − | |||
| − | |||
Latest revision as of 23:22, 14 November 2026
Contents
while
Instruction. Repeats a statement as long as a condition is true.
while (condition) statement
Tests condition, and while it is true runs the statement, usually a block in
{ }, and tests again. break leaves the loop at once;
continue goes back to the test.
Notes
- The condition is true when it is a non-zero integer, a unit or string that isn't null, or a list that isn't empty. A condition that fails (a field of a null unit) is false, so
while (u.level > 5)stops whenubecomes null. - Don't test a text field or a string list entry on its own, as in
while (sl.[i])orwhile (u.title): for these the test is the wrong way round, true when the text is empty. Writewhile (sl.[i] != ""). andandorevaluate both sides:(u != null) and (u.type == UNIT_ST_PC)still readsu.type. That is harmless here, since a failed field is false.- There is no
do ... whileand nofor. - A loop that runs about 52000 instructions without a
waitorpausegets the program stopped ("had endless loop" in the zone log). A long walk over the world's units needs apausenow and then. - A loop over a room's units that moves or destroys them should be a foreach(): after
link(u, ...),u.nextis in the other place.
Example
dilbegin unitptr first_weapon(ch : unitptr);
var
u : unitptr;
code
{
u := ch.inside;
while (u != null)
{
if (u.type == UNIT_ST_OBJ)
if (u.objecttype == ITEM_WEAPON)
break;
u := u.next;
}
return(u); // null when there is none
}
dilend
Finds the first weapon a character carries, or null.