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 for Loop

The for loop allows you to do one or more statements a specific number of times. This allows you to repeat programming commands an exact number of times in any program.


The for Loop

The basic form of the for loop is shown below:

for(initial-expression; conditional-expression; loop-expression)

 {

   (body of loop)

 }

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

 

//Displays the message "Hello there five times from the

//count of 1 to 5.

void main()

 {

  int Counter;

  string Message;

  object oPC=GetEnteringObject();

   Message="Hello there!";

   for(Counter=1; Counter<=5; Counter++)

   {

     Floating TextStringOnCreature(Message, oPC);

   }

}

 


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

 

From the Program for loop meaning Comments
for for Beginning of the for loop.
Counter=1 initial-expression The starting conditions in the for loop
counter. Counter is any integer variable
and its starting value is set to 1 in
this program. Its starting value can be set to
any amount including negative numbers.
Counter<=5 conditional-expression What the condition is for the for loop. In
this example the for loop will continue to
repeat the body of the loop while the value
of the variabble Counter is less than or equal to 5.
Count++ loop-expression In this example, the loop-expression
increments the variable Counter one time
-- -- --
{ { The opening brace where the body of the for loop begins.
} } The closing brace of the body of the for loop.
Statements between the opening brace
and the closing brace will be executed as
long as the for loop is active.

 


 

Where do we go from here?

The next sections will introduce the while loop.

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.