Break: Difference between revisions

From ZDoom Wiki
Jump to navigation Jump to search
Content deleted Content added
m Remove <code> tags
Deded007 (talk | contribs)
m formatting
 
(2 intermediate revisions by 2 users not shown)
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 iteration 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==


This example breaks out of the current for loop iteration if the matching player is not currently in the game.
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++)
for (int i = 0; i < 8; i++)
Line 17: Line 17:
}
}


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


while (PlayerDrugged)
while (PlayerDrugged)
Line 23: Line 23:
[[delay]] (35);
[[delay]] (35);
if ([[CheckInventory]] ("[[Classes:RadSuit|RadSuit]]"))
if ([[CheckInventory]] ("{{Class|RadSuit}}"))
[[break]]; // Radiation suit protects against drugged effect
[[break]]; // Radiation suit protects against drugged effect
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]] ("[[Classes:ZombieMan|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]] ("[[Classes:DoomImp|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]] ("[[Classes:BaronOfHell|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]] ("[[Classes:DoomImp|DoomImp]]", 61);
[[SpawnSpot]] ("{{Class|DoomImp}}", 61);
[[break]];
[[break]];
case SKILL_VERY_HARD:
case {{const|SKILL_VERY_HARD}}:
// Spawn a CYBERDEMON!
// Spawn a CYBERDEMON!
[[SpawnSpot]] ("[[Classes:Cyberdemon|Cyberdemon]]", 60);
[[SpawnSpot]] ("{{Class|Cyberdemon}}", 60);
[[break]];
[[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;
}