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 - Case

THe 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:

switch (expressions1)

 {

  case constant-expressions : (statements1)

  break;

  default: (statements2)

 }

An example of the use of the switch-case statment is shown in the following program:

//Checks the resulting value of a random

//generated number from 1 to 4 and awards

//or takes gold from creature.

void main()

 {

  object oPC=GetEnteringObject();

  string Message;

  int Gold;

  int Roll;

   Roll=d4( );

   switch (Roll)

   {

    case 1 : //A one is rolled.

             Message="You rolled a one and get 10 gold.";

             Gold=10;

             GiveGoldToCreature(oPC, Gold);

   break;

    case 2 : //A two is rolled.

             Message="You rolled a two and get 5 gold.";

             Gold=5;

             GiveGoldToCreature(oPC, Gold);

   break;

    case 3 : //A three is rolled.

             Message="You rolled a three and get nothing.";

   break;

    case 4 : //A four is rolled.

             Message="You rolled a four and lose 5 gold.";

             Gold=5;

             TakeGoldFromCreature(Gold, oPC);

   break;

    default : //None of the above happens.

             Message="That was not a vlid result.";

   }

FloatingTextStringOnCreature(Message, oPC);

}

 


 

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:

 

switch (expression)

   {

     case constant-expression: (staatements1)

     break;

     default: (statements2)

   }

 

Statment Meaning
switch Reserved word indicating that a switched statment is about to take place.
switch(expression) expression is the variable whose value will be tested - Must be a single value, no type float
{ Defines the beginning of the switchbody.
case Reserved word indiating that what follows is the value ofexpressionreqruied for a match.  
constant-expression The actual value of the case variable required for a match.
: Required colon indicating that the statements to be executed for a match follow.
statements1 The statements that iwll be executed for this case match.  
break; The reserved word indicating where hte statments to be executed for this case match are to end. Note that there is a semicolon after the break
default: Reserved wod indicating to execute waht follows if no match is found. Note the required colon.
statements2 The statments to be executed if the default is activatd.
} The ending of the switch body.

 


 

Where do we go from here?

THe next sections will introduce iterations, that is how we can repeat instructions.

Nwn2Scripting provides material for training only. We do not warrant the correctness of its contents. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 2008 by Adamson House, Ltd. All Rights Reserved.

Questions or Comments: EMail Webmaster

Donations are to Adamson House, Ltd who maintains this site.
All donations go the the improvement of this site.