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

The do while loop

The do while loop is structured simular to the while loop, the difference being that the test condition is evaluated after the loop is executed. Recall that the while loop tests the condtion before the loop is executed.


The do while loop

The basic form of the do while loop is shown below:

do

 {

  (body of loop)

 }

while(expression);

An example of the use of the do while loop is shown in the following program:

 

void main()

{

  int TheTime;

  string Message;

  object oPC=GetEnteringObject();

   Message="Booooo!";

    do

     {

       TheTime=GetTimeHour();

     FloatingTextStringOnCreature(Message, oPC);

      }    while(TheTIme==12)

}

 


 

The above program checks the current game time (the game hour). If the game time is 12 (midnight) then the while-loop initiates and keeps checking the game time. As long as the game time is equal to 12, the message "Boooo!" keeps appearing. Once the game time hour is no longer equal to 12, the while-loop stops. Because the while loop always executes once (the first time) one "Boooo!" is always displayed.

The table below identifies the major parts of the do while loop from the program above.

From the program do while loop meaning
do Required reservved word to begin the do while loop.
{ Beginning of the do while loop body.
} End of the do while loop body.
while Reserved word indicating that a test condition is to follow.
(TheTime==12) Expression of the do while loop test, where hte loop is re-executed if TRUE.

 


 

Where do we go from here?

The next section introduces nested loops.

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.