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