Difference between revisions of "Break Statement"

From NWN Lexicon
Jump to: navigation, search
(Created page with "=break Statement= Used to escape from a looping structure ( switch case statement. <br /> If a break statement is executed from within a ...")
 
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=break Statement=
 
  
Used to escape from a looping structure ([[ switch_case_Statement | switch ]] case statement.
+
Used to escape from a looping structure ([[do Loop|do]], [[while Loop|while]], and [[for Loop|for]]) or [[Switch_case_Statement|switch]] case statement.
 
 
<br />
 
  
 
If a break statement is executed from within a looping code block the loop will halt execution regardless of the condition controlling the loop. Any remaining statements within the code block will not be executed.
 
If a break statement is executed from within a looping code block the loop will halt execution regardless of the condition controlling the loop. Any remaining statements within the code block will not be executed.
 
<br />
 
  
 
If a break statement is executed from within a case inside of a switch statement, all following case statements are ignored.
 
If a break statement is executed from within a case inside of a switch statement, all following case statements are ignored.
 
<br />
 
  
 
The following example searches for a PC with a strength score higher than 12, and repeats until there are no more PCs to search:
 
The following example searches for a PC with a strength score higher than 12, and repeats until there are no more PCs to search:
 +
<nwscript>
 +
object oTarget = GetFirstPC();
  
+
while (oTarget != OBJECT_INVALID)  
<font color="#0000ff">object</font> oTarget = GetFirstPC();
+
{
+
    int nStr = GetAbilityScore(oTarget, ABILITY_STRENGTH);
<font color="#0000ff">while</font> (oTarget != <font color="#0000ff">OBJECT_INVALID</font>)  
+
    if (nStr > 12)  
{
+
        break;
      <font color="#0000ff">int</font> nStr = GetAbilityScore(oTarget, ABILITY_STRENGTH);
 
      <font color="#0000ff">if</font> (nStr &gt; <font color="#7f007f">12</font>)  
 
          <font color="#0000ff">break</font>;
 
      oTarget = GetNextPC();
 
}
 
  
As soon as the loop finds a PC with a strength higher than 12 the break statement is called halting the loop and the search for a target. Note that if no PCs are present with a strength score higher than 12, the loop will not exit (seems to be a problem with GetNextPC).
+
    oTarget = GetNextPC();
 +
}
 +
</nwscript>
  
<br /><br />
+
As soon as the loop finds a PC with a strength higher than 12 the break statement is called halting the loop and the search for a target. Note that if no PCs are present with a strength score higher than 12, the loop will not exit (seems to be a problem with [[GetNextPC]]()).
 
----
 
----
<font color="gray">  author: Ryan Hunt, editor: Charles Feduke, Mistress, additional contributor(s): Rich Dersheimer, Box, Scott Smith, bug finder: Pondo</font><br />  <br />
+
 
 +
<font color="gray">author: Ryan Hunt, editor: Charles Feduke, Mistress, additional contributor(s): Rich Dersheimer, Box, Scott Smith, bug finder: Pondo</font>
 
[[Category:Primers]]
 
[[Category:Primers]]
 
[[Category:Conditionals_and_Loops]]
 
[[Category:Conditionals_and_Loops]]
 +
{{DISPLAYTITLE:break Statement}}

Latest revision as of 04:45, 24 December 2017

Used to escape from a looping structure (do, while, and for) or switch case statement.

If a break statement is executed from within a looping code block the loop will halt execution regardless of the condition controlling the loop. Any remaining statements within the code block will not be executed.

If a break statement is executed from within a case inside of a switch statement, all following case statements are ignored.

The following example searches for a PC with a strength score higher than 12, and repeats until there are no more PCs to search:

object oTarget = GetFirstPC();
 
while (oTarget != OBJECT_INVALID) 
{
    int nStr = GetAbilityScore(oTarget, ABILITY_STRENGTH);
    if (nStr > 12) 
        break;
 
    oTarget = GetNextPC();
}

As soon as the loop finds a PC with a strength higher than 12 the break statement is called halting the loop and the search for a target. Note that if no PCs are present with a strength score higher than 12, the loop will not exit (seems to be a problem with GetNextPC()).


author: Ryan Hunt, editor: Charles Feduke, Mistress, additional contributor(s): Rich Dersheimer, Box, Scott Smith, bug finder: Pondo