If else Statement

From NWN Lexicon
Revision as of 21:48, 25 August 2012 by Dood (talk | contribs)
Jump to: navigation, search

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