Difference between revisions of "If else Statement"

From NWN Lexicon
Jump to: navigation, search
 
(4 intermediate revisions by 4 users not shown)
Line 4: Line 4:
  
 
<nwscript>
 
<nwscript>
+
 
int bAlpha = FALSE;
+
int bAlpha = FALSE;
int bBravo = FALSE;
+
int bBravo = FALSE;
if (bAlpha)  
+
if (bAlpha)  
{
+
{
      // statement 1 ...
+
    // statement 1 ...
}  
+
}  
else if (bBravo)  
+
else if (bBravo)  
{
+
{
      // statement 2 ...
+
    // statement 2 ...
}
+
}
else
+
else
{
+
{
      // statement 3 ...
+
    // statement 3 ...
      // in this example, this is the code that is executed becase
+
    // in this example, this is the code that is executed becase
      // both bAlpha and bBravo are FALSE
+
    // both bAlpha and bBravo are FALSE
}
+
}
 
</nwscript>
 
</nwscript>
  
Line 30: Line 30:
  
 
<nwscript>
 
<nwscript>
+
 
int bCondition = TRUE;
+
int bCondition = TRUE;
if (bCondition) PrintString("TRUE!");
+
if (bCondition) PrintString("TRUE!");
 
</nwscript>
 
</nwscript>
  

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