Difference between revisions of "Manual:DIL Manual/while()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="dcwhile"></span>
+
= while =
−
'''while:'''
+
'''Instruction.''' Repeats a statement as long as a condition is true.
−
    The while statement lets you execute a series of
 
−
    statements while an expression evaluates to TRUE.
 
  
−
  '''Example:'''
+
  while (condition)
 +
    statement
  
−
<nowiki>
+
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.
  
−
dilbegin foo();
+
== Notes ==
−
  code
+
* 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.
−
dilend</nowiki>
+
* 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 ==
−
<span id="dcwhile"></span>
+
<pre>
−
'''while:'''
+
dilbegin unitptr first_weapon(ch : unitptr);
−
    The while statement lets you execute a series of
+
var
−
    statements while an expression evaluates to TRUE.
+
  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.
  
−
'''Example:'''
+
== See also ==
−
 
+
* [[Manual:DIL Manual/break|break]]
−
<nowiki>
+
* [[Manual:DIL Manual/continue|continue]]
−
 
+
* [[Manual:DIL Manual/foreach()|foreach()]]
−
dilbegin foo();
+
* [[Manual:DIL Manual/if()|if()]]
−
  code
+
* [[Manual:DIL Manual/goto|goto]]
−
dilend</nowiki>
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

Latest revision as of 23:22, 14 November 2026

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 when u becomes null.
  • Don't test a text field or a string list entry on its own, as in while (sl.[i]) or while (u.title): for these the test is the wrong way round, true when the text is empty. Write while (sl.[i] != "").
  • and and or evaluate both sides: (u != null) and (u.type == UNIT_ST_PC) still reads u.type. That is harmless here, since a failed field is false.
  • There is no do ... while and no for.
  • A loop that runs about 52000 instructions without a wait or pause gets the program stopped ("had endless loop" in the zone log). A long walk over the world's units needs a pause now and then.
  • A loop over a room's units that moves or destroys them should be a foreach(): after link(u, ...), u.next is 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.

See also