Difference between revisions of "Manual:DIL Manual/break"

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="dcbreak"></span>
+
= break =
−
'''break:'''
+
'''Instruction.''' Leaves the loop it is in.
−
    The break statement makes you break out of any
 
−
    loop you're currently in.
 
  
−
  '''Example:'''
+
  break;
  
−
<nowiki>
+
Jumps to the first statement after the innermost [[Manual:DIL Manual/while()|while]] or [[Manual:DIL Manual/foreach()|foreach]] around it. These are DIL's only loop statements.
  
−
dilbegin foo();
+
== Notes ==
−
  code
+
* Outside a loop vmc stops with "Using break outside loop".
−
dilend</nowiki>
+
* In nested loops it only leaves the innermost one.
 +
* Leaving a <code>foreach</code> early is fine: the units it hadn't reached are dropped when the next <code>foreach</code> starts or the template returns.
  
−
---~---~---~---~---~---~---~---~---
+
== Example ==
−
<span id="dcbreak"></span>
+
<pre>
−
'''break:'''
+
dilbegin wake_sleeper();
−
    The break statement makes you break out of any
+
var
−
    loop you're currently in.
+
  u : unitptr;
 +
code
 +
{
 +
  u := self.outside.inside;
 +
  while (u != null)
 +
  {
 +
      if ((u.type == UNIT_ST_PC) and (u.position == POSITION_SLEEPING))
 +
        break;
 +
      u := u.next;
 +
  }
 +
  if (u != null)
 +
      exec("wake " + u.name, self);
 +
  quit;
 +
}
 +
dilend
 +
</pre>
 +
An NPC looks through its room for the first sleeping player and wakes them. After the loop <code>u</code> is that player, or null when there was none.
  
−
'''Example:'''
+
== See also ==
−
 
+
* [[Manual:DIL Manual/continue|continue]]
−
<nowiki>
+
* [[Manual:DIL Manual/while()|while()]]
−
 
+
* [[Manual:DIL Manual/foreach()|foreach()]]
−
dilbegin foo();
 
−
  code
 
−
dilend</nowiki>
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

Latest revision as of 23:16, 14 November 2026

break

Instruction. Leaves the loop it is in.

break;

Jumps to the first statement after the innermost while or foreach around it. These are DIL's only loop statements.

Notes

  • Outside a loop vmc stops with "Using break outside loop".
  • In nested loops it only leaves the innermost one.
  • Leaving a foreach early is fine: the units it hadn't reached are dropped when the next foreach starts or the template returns.

Example

dilbegin wake_sleeper();
var
   u : unitptr;
code
{
   u := self.outside.inside;
   while (u != null)
   {
      if ((u.type == UNIT_ST_PC) and (u.position == POSITION_SLEEPING))
         break;
      u := u.next;
   }
   if (u != null)
      exec("wake " + u.name, self);
   quit;
}
dilend

An NPC looks through its room for the first sleeping player and wakes them. After the loop u is that player, or null when there was none.

See also