Break: Difference between revisions

From ZDoom Wiki
Jump to navigation Jump to search
Content deleted Content added
Tsrow (talk | contribs)
m added description
 
Deded007 (talk | contribs)
m formatting
 
(7 intermediate revisions by 6 users not shown)
Line 1: Line 1:
break;
Break jumps out of the current do, for or while iteration Statement or from a switch statement.

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

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