Manual:DIL Manual/else()

From DikuMUD Wiki
Jump to navigation Jump to search

else()

Instruction. The second branch of an if.

if (condition) statement1 else statement2

else is part of if and can't stand alone. statement2 runs when the condition is false. It can be a single statement, a block in braces, or another if, which makes an else if chain.

Notes

  • No ; between a closing brace and else: if (a) { ... }; else ... is a syntax error at else. After a single statement the ; is fine: if (a) x := 1; else x := 2;.
  • An else belongs to the nearest if before it. In if (a) if (b) x := 1; else x := 2; the else goes with if (b); put the inner if in braces if it should go with if (a).

Example

dilbegin rate_hp(pc : unitptr);
code
{
   if (pc.hp > pc.max_hp / 2)
      act("$1n looks healthy.", A_ALWAYS, pc, null, null, TO_ROOM);
   else if (pc.hp > 0)
      act("$1n is wounded.", A_ALWAYS, pc, null, null, TO_ROOM);
   else
   {
      act("$1n is dying.", A_ALWAYS, pc, null, null, TO_ROOM);
      act("You are dying.", A_ALWAYS, pc, null, null, TO_CHAR);
   }
   return;
}
dilend

Tells the room how a character is doing, with an else if chain.

See also