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

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="dcif"></span>
+
= if() =
−
'''if:'''
+
'''Instruction.''' Runs a statement only when a condition holds, with an optional <code>else</code>.
−
    The if statement is much like C. It takes any type as
 
−
    argument. Non integers are considered 'TRUE' if they are
 
−
    not null.
 
  
−
  '''Example:'''
+
  if (condition) statement
 +
if (condition) statement else statement
  
−
<nowiki>
+
The condition goes in parentheses. The statement is one statement ending in <code>;</code>, or a block in braces <code>{ ... }</code>, or another <code>if</code>. When the condition is false and there is an <code>else</code>, the statement after <code>else</code> runs instead.
  
−
  dilbegin foo();
+
The condition can be of any type:
−
  code
+
* integer: true when it isn't 0;
−
dilend</nowiki>
+
* unitptr, extraptr, zoneptr, cmdptr and string: true when it isn't null;
 +
* stringlist and intlist: true when the list has at least one element;
 +
* a failed value (a field of a null unit, such as <code>u.level</code> when <code>u</code> is null): false.
  
−
'''Example:'''
+
== Notes ==
 +
* No <code>;</code> between a closing brace and <code>else</code>: <code>if (a) { ... }; else ...</code> is a syntax error at <code>else</code>. After a single statement, <code>if (a) x := 1; else x := 2;</code> is fine.
 +
* <code>else if (...)</code> makes a chain. An <code>else</code> belongs to the nearest <code>if</code> before it; use braces when <code>if</code> statements are nested.
 +
* <code>and</code> and <code>or</code> evaluate both sides; there is no short cut. To keep away from a null unit, test it in an <code>if</code> of its own first.
 +
* Test text by comparing it: <code>if (s != "")</code>. A bare string variable is true as soon as it holds a value, even <code>""</code>. The fields <code>title</code>, <code>descr</code>, <code>outside_descr</code> and <code>inside_descr</code> tested bare are currently true when they are '''empty''' (a bug), so compare them with <code>""</code> as well.
 +
* String <code>==</code> and <code>!=</code> ignore case.
  
−
<nowiki>
+
== Example ==
 +
<pre>
 +
dilbegin spirit_only();
 +
code
 +
{
 +
  :start:
 +
  wait(SFB_CMD, activator.type == UNIT_ST_PC);
 +
  if (activator.level >= ADMINISTRATOR_LEVEL)
 +
      goto start;
  
−
   dilbegin foo();
+
   if (not isset(activator.pcflags, PC_SPIRIT))
−
   code
+
  {
−
dilend</nowiki>
+
      act("Only the dead may stay here.", A_ALWAYS, activator, null, null, TO_CHAR);
 +
      link(activator, findroom(activator.hometown));
 +
      block;
 +
  }
 +
  else if (command("cast"))
 +
  {
 +
      act("Your powers do not work in the spirit world!",
 +
          A_ALWAYS, activator, null, null, TO_CHAR);
 +
      block;
 +
   }
 +
  goto start;
 +
}
 +
dilend
 +
</pre>
 +
A room where only dead players may stay, and where spells don't work. Adapted from basis.zon.
  
−
'''Example:'''
+
== See also ==
−
 
+
* [[Manual:DIL Manual/else()|else()]]
−
<nowiki>
+
* [[Manual:DIL Manual/while()|while()]]
−
 
+
* [[Manual:DIL Manual/goto|goto]]
−
  dilbegin foo();
+
* [[Manual:DIL Manual/on()|on()]]
−
  code
+
* [[Manual:DIL Manual/Operators|Operators]]
−
dilend</nowiki>
+
* [[Manual:DIL Manual/DIL Constructs|DIL Constructs]]
−
 
 
−
---~---~---~---~---~---~---~---~---
 
−
<span id="dcif"></span>
 
−
'''if:'''
 
−
    The if statement is much like C. It takes any type as
 
−
    argument. Non integers are considered 'TRUE' if they are
 
−
    not null.
 
−
 
 
−
'''Example:'''
 
−
 
 
−
<nowiki>
 
−
 
 
−
  dilbegin foo();
 
−
  code
 
−
dilend</nowiki>
 
−
 
 
−
'''Example:'''
 
−
 
 
−
<nowiki>
 
−
 
 
−
  dilbegin foo();
 
−
  code
 
−
dilend</nowiki>
 
−
 
 
−
'''Example:'''
 
−
 
 
−
<nowiki>
 
−
 
 
−
  dilbegin foo();
 
−
  code
 
−
dilend</nowiki>
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

Latest revision as of 23:22, 14 November 2026

if()

Instruction. Runs a statement only when a condition holds, with an optional else.

if (condition) statement
if (condition) statement else statement

The condition goes in parentheses. The statement is one statement ending in ;, or a block in braces { ... }, or another if. When the condition is false and there is an else, the statement after else runs instead.

The condition can be of any type:

  • integer: true when it isn't 0;
  • unitptr, extraptr, zoneptr, cmdptr and string: true when it isn't null;
  • stringlist and intlist: true when the list has at least one element;
  • a failed value (a field of a null unit, such as u.level when u is null): false.

Notes

  • No ; between a closing brace and else: if (a) { ... }; else ... is a syntax error at else. After a single statement, if (a) x := 1; else x := 2; is fine.
  • else if (...) makes a chain. An else belongs to the nearest if before it; use braces when if statements are nested.
  • and and or evaluate both sides; there is no short cut. To keep away from a null unit, test it in an if of its own first.
  • Test text by comparing it: if (s != ""). A bare string variable is true as soon as it holds a value, even "". The fields title, descr, outside_descr and inside_descr tested bare are currently true when they are empty (a bug), so compare them with "" as well.
  • String == and != ignore case.

Example

dilbegin spirit_only();
code
{
   :start:
   wait(SFB_CMD, activator.type == UNIT_ST_PC);
   if (activator.level >= ADMINISTRATOR_LEVEL)
      goto start;

   if (not isset(activator.pcflags, PC_SPIRIT))
   {
      act("Only the dead may stay here.", A_ALWAYS, activator, null, null, TO_CHAR);
      link(activator, findroom(activator.hometown));
      block;
   }
   else if (command("cast"))
   {
      act("Your powers do not work in the spirit world!",
          A_ALWAYS, activator, null, null, TO_CHAR);
      block;
   }
   goto start;
}
dilend

A room where only dead players may stay, and where spells don't work. Adapted from basis.zon.

See also