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)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 +
= break =
 +
'''Instruction.''' Leaves the loop it is in.
  
 +
break;
  
−
<span id="dcbreak"></span>
+
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.
−
'''break:'''
 
−
    The break statement makes you break out of any
 
−
    loop you're currently in.
 
  
−
'''Example:'''
+
== Notes ==
 +
* Outside a loop vmc stops with "Using break outside loop".
 +
* 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.
  
−
<nowiki>
+
== Example ==
−
 
+
<pre>
−
dilbegin foo();
+
dilbegin wake_sleeper();
−
   code
+
var
 +
   u : unitptr;
 +
code
 +
{
 +
  u := self.outside.inside;
 +
  while (u != null)
 
   {
 
   {
−
    while (self.inside) {
+
      if ((u.type == UNIT_ST_PC) and (u.position == POSITION_SLEEPING))
−
      if (self.position &lt POSITION_SLEEPING)
 
 
         break;
 
         break;
−
      exec("say I own something", self);
+
      u := u.next;
−
      pause;
 
−
    }
 
 
   }
 
   }
−
   dilend</nowiki>
+
   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.
  
−
---~---~---~---~---~---~---~---~---
+
== See also ==
 +
* [[Manual:DIL Manual/continue|continue]]
 +
* [[Manual:DIL Manual/while()|while()]]
 +
* [[Manual:DIL Manual/foreach()|foreach()]]

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