Break: Difference between revisions

From ZDoom Wiki
Jump to navigation Jump to search
Content deleted Content added
HotWax (talk | contribs)
No edit summary
Deded007 (talk | contribs)
m Fixed Typo
Line 55: Line 55:
case SKILL_VERY_HARD:
case SKILL_VERY_HARD:
// Spawn a CYBERDEMON!
// Spawn a CYBERDEMON!
[[SpawnSpot]] ("[[Classes:Cyberdemon|Cyberdemo]]", 60);
[[SpawnSpot]] ("[[Classes:Cyberdemon|Cyberdemon]]", 60);
[[break]];
[[break]];
}
}

Revision as of 02:12, 14 December 2008

break;

Usage

Break is used to exit from a block of code early, and is most commonly used to break out of the current iteration of a do, for or while statement, or to break out of a switch block completely.

Examples

This example breaks out of the current for loop iteration 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 current while loop iteration 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;
}