Break

From ZDoom Wiki
Jump to: navigation, search
break;

Usage

break is used to exit from a (given type of) block of code early, and is most commonly used to break out of the current scope of a do, for, or while statement, or to break out of a switch block completely.

Examples

This example breaks out of the for loop if the matching player is not currently in the game.

for (int i = 0; i < 8; i++)
{
   if (!PlayerInGame (i))
      break;

   TeleportOther (1000 + i, 60 + i, 1);
}

This example breaks out of the while loop if the player has the radiation suit.

while (PlayerDrugged)
{
   delay (35);

   if (CheckInventory ("RadSuit"))
      break; // Radiation suit protects against drugged effect

   FadeTo (random (0, 2) * 128, random (0, 2) * 128, random (0, 2) * 128, 1.0, 1.0);
}

This example uses the break statement multiple times in a select block. It is important to remember to use break at the end of each case to avoid execution “falling through” to the next case block.

switch (GameSkill ())
{
   case SKILL_VERY_EASY:
      // Spawn one zombie. Pathetically easy.
      SpawnSpot ("ZombieMan", 60);
      break;

   case SKILL_EASY:
      // Spawn one imp.  Still really easy.
      SpawnSpot ("DoomImp", 60);
      break;

   case SKILL_HARD:
      // Spawn a baron, in addition to three imps.
      SpawnSpot ("BaronOfHell", 60);
      // break is intentionally not used here, to allow execution to continue through the next block.

   case SKILL_NORMAL:
      // Spawn three imps.
      SpawnSpot ("DoomImp", 61);
      break;

   case SKILL_VERY_HARD:
      // Spawn a CYBERDEMON!
      SpawnSpot ("Cyberdemon", 60);
      break;
}