| NWN2 Scripting. |
Simulation Training. Introduction Home First Program Seeing Results Variables and Data Types Comments Functions Data Conversion Random Numbers Concatenation Arithmetic Operators Compound Assignments One More or Less Precedence Relational Operators The Open Branch The Closed Branch Logical AND Logical OR Compound Statements ELSE If Switch Case The ? Operator The for Loop The while Loop The do while Loop Introduction to Functions Passing Values Passing Multiple Values Multiple Functions Simplifying Functions TRUE/fALSE Conditions Return Values Setting Global Variables Getting Global Variables Setting Local Variables Getting Local Variables WayPoints Introduction Static Waypoint Sets Dynamic Waypoint Sets Dynamic WP Cycles Input Output |
Switch - CaseTHe switch-case statement is used when you have several options and only one of them is to e selected. The ability to do this presents on of the most powerful selection operations in this programming language.
The switch-case The switch-case statement is an easier way to code multiple if..else if..else statements. The switch statement has the form shown below:
An example of the use of the switch-case statment is shown in the following program:
The construction of the switch-case statement requires the introduction of three other key words: case, switch and default. Essentially, the switch statement identifies a variable whoses value will determine which case will be activated. As you can see from the above program, the variable Roll (which will contain the integer value of the random number), acts as the variable to be tested. The values folllowing each of the case statements (such as 1) represent one of the value the variable Roll can have. The break statement lets the program know when the statements following the switch statment no longer belong to that switch statment. If no break statment is included, the program will fall through to the next case and execute those statment as well. The table below identifies the major components of the switch-case statment:
Where do we go from here? THe next sections will introduce iterations, that is how we can repeat instructions.
|
|