Difference between revisions of "If else Statement"

From NWN Lexicon
Jump to: navigation, search
(Created page with "=if/else Statement= if statements are used to control the flow of code execution. This is best illustrated with an example: <font color="#0000ff">int</font> bAlpha =...")
 
 
(5 intermediate revisions by 4 users not shown)
Line 3: Line 3:
 
if statements are used to control the flow of code execution. This is best illustrated with an example:
 
if statements are used to control the flow of code execution. This is best illustrated with an example:
  
+
<nwscript>
<font color="#0000ff">int</font> bAlpha = <font color="#0000ff">FALSE</font>;
+
 
<font color="#0000ff">int</font> bBravo = <font color="#0000ff">FALSE</font>;
+
int bAlpha = FALSE;
<font color="#0000ff">if</font> (bAlpha)  
+
int bBravo = FALSE;
{
+
if (bAlpha)  
      <font color="#007f00">// statement 1 ...
+
{
</font>}  
+
    // statement 1 ...
<font color="#0000ff">else</font> <font color="#0000ff">if</font> (bBravo)  
+
}  
{
+
else if (bBravo)  
      <font color="#007f00">// statement 2 ...
+
{
</font>}
+
    // statement 2 ...
<font color="#0000ff">else</font>
+
}
{
+
else
      <font color="#007f00">// statement 3 ...
+
{
</font>    <font color="#007f00">// in this example, this is the code that is executed becase
+
    // statement 3 ...
</font>    <font color="#007f00">// both bAlpha and bBravo are FALSE
+
    // in this example, this is the code that is executed becase
</font>}
+
    // both bAlpha and bBravo are FALSE
 +
}
 +
</nwscript>
  
 
If ''bAlpha'' evaluates to TRUE then ''statement 1'' will execute. If it evaluates to FALSE then it will skip to the next ''if else'' and evaluate ''bBravo''. If neither ''bAlpha'' or ''bBravo'' evaluates to TRUE then ''statement 3'' will execute as the default (catch-all).
 
If ''bAlpha'' evaluates to TRUE then ''statement 1'' will execute. If it evaluates to FALSE then it will skip to the next ''if else'' and evaluate ''bBravo''. If neither ''bAlpha'' or ''bBravo'' evaluates to TRUE then ''statement 3'' will execute as the default (catch-all).
Line 27: Line 29:
 
if statements can also be used alone without ''if else'' or ''else'' statements:
 
if statements can also be used alone without ''if else'' or ''else'' statements:
  
+
<nwscript>
<font color="#0000ff">int</font> bCondition = <font color="#0000ff">TRUE</font>;
+
 
<font color="#0000ff">if</font> (bCondition) PrintString(<font color="#00007f">"TRUE!"</font>);
+
int bCondition = TRUE;
 +
if (bCondition) PrintString("TRUE!");
 +
</nwscript>
  
 
If ''bCondition'' evaluates to FALSE it will skip ''PrintString("TRUE!")'' and move on to the next line of code.
 
If ''bCondition'' evaluates to FALSE it will skip ''PrintString("TRUE!")'' and move on to the next line of code.

Latest revision as of 22:35, 23 December 2017

if/else Statement

if statements are used to control the flow of code execution. This is best illustrated with an example:

int bAlpha = FALSE;
int bBravo = FALSE;
if (bAlpha) 
{
     // statement 1 ...
} 
else if (bBravo) 
{
     // statement 2 ...
}
else
{
     // statement 3 ...
     // in this example, this is the code that is executed becase
     // both bAlpha and bBravo are FALSE
}

If bAlpha evaluates to TRUE then statement 1 will execute. If it evaluates to FALSE then it will skip to the next if else and evaluate bBravo. If neither bAlpha or bBravo evaluates to TRUE then statement 3 will execute as the default (catch-all).


if statements can also be used alone without if else or else statements:

int bCondition = TRUE;
if (bCondition) PrintString("TRUE!");

If bCondition evaluates to FALSE it will skip PrintString("TRUE!") and move on to the next line of code.





 author: Ryan Hunt, editor: Charles Feduke