Break: Difference between revisions

From ZDoom Wiki
Jump to navigation Jump to search
Content deleted Content added
Deded007 (talk | contribs)
m Minor wording change
Deded007 (talk | contribs)
m formatting
 
Line 3: Line 3:
==Usage==
==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.
'''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==
==Examples==
Line 29: Line 29:
}
}


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.
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]] ())
switch ([[GameSkill]] ())
{
{
case SKILL_VERY_EASY:
case {{const|SKILL_VERY_EASY}}:
// Spawn one zombie. Pathetically easy.
// Spawn one zombie. Pathetically easy.
[[SpawnSpot]] ("{{Class|ZombieMan}}", 60);
[[SpawnSpot]] ("{{Class|ZombieMan}}", 60);
[[break]];
[[break]];
case SKILL_EASY:
case {{const|SKILL_EASY}}:
// Spawn one imp. Still really easy.
// Spawn one imp. Still really easy.
[[SpawnSpot]] ("{{Class|DoomImp}}", 60);
[[SpawnSpot]] ("{{Class|DoomImp}}", 60);
[[break]];
[[break]];
case SKILL_HARD:
case {{const|SKILL_HARD}}:
// Spawn a baron, in addition to three imps.
// Spawn a baron, in addition to three imps.
[[SpawnSpot]] ("{{Class|BaronOfHell}}", 60);
[[SpawnSpot]] ("{{Class|BaronOfHell}}", 60);
// break is intentionally not used here, to allow execution to continue through the next block.
// break is intentionally not used here, to allow execution to continue through the next block.
case SKILL_NORMAL:
case {{const|SKILL_NORMAL}}:
// Spawn three imps.
// Spawn three imps.
[[SpawnSpot]] ("{{Class|DoomImp}}", 61);
[[SpawnSpot]] ("{{Class|DoomImp}}", 61);
[[break]];
[[break]];
case SKILL_VERY_HARD:
case {{const|SKILL_VERY_HARD}}:
// Spawn a CYBERDEMON!
// Spawn a CYBERDEMON!
[[SpawnSpot]] ("{{Class|Cyberdemon}}", 60);
[[SpawnSpot]] ("{{Class|Cyberdemon}}", 60);

Latest revision as of 19:24, 10 February 2013

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;
}