Loop

A loop is a programming structure that causes a statement or statements (the loop body) to be executed multiple times, typically until a conditional expression is false. This can be used both to reduce the amount of redundant code in a script and to handle a situation when the number of iterations through the loop will be unknown until execution time.

When writing a loop, an important consideration is the updating of the loop, which refers to the setting of the variables used in the loop conditional so that the conditional eventually terminates the loop. Updating the loop is often the first or last thing done in the loop body so that this step is not forgotten. Failure to update the loop typically results in an infinite loop &mdash; a loop that never ends. Neverwinter Nights has a safeguard in place to prevent scripts from performing infinite loops, specifically a limit on the number of instructions a single script can execute. When this limit is broken, the game ends the script prematurely and reports "TOO MANY INSTRUCTIONS" to the player(s). There are a few cases where this "TMI" error is reported for other reasons, but usually it indicates the existence of an infinite loop.

A related important consideration is the initialization of the loop. Failure to set the initial value of the variables used in the conditional will more often cause a loop to execute too few times than infinitely too many, but the end result is still not what was intended.

In NWScript
NWScript provides three loop statements:,  , and.

while
The simplest of NWScript's loop statements is the  statement, which takes the following form.

while ( conditional ) instruction

The instruction (the loop body) can be either a single statement or a block of statements enclosed in curly braces. This structure first checks the conditional. If the conditional is a true value, the instruction is executed then the conditional is checked again. This is repeated until the conditional is a false value, in which case execution drops to the line following the loop.

do...while
The  statement is similar to the , except that its body is executed before the first check of the conditional. (So the loop body is always executed at least once, whereas the loop body of a  statement could possibly never be executed.) To reflect this, the conditional appears after the instruction, giving the statement the following form.

do    instruction while ( conditional )

As with the  statement, the instruction can be either a single statement or a block of statements enclosed in curly braces.

for
The  statement has a more complex structure, but it can be more convenient in certain situations, particularly when a loop is to iterate a fixed number of times. The  statement takes the following form.

for ( initialize ; conditional ; update ) instruction

The initialize and update parts can be either blank or single statements, while the instruction part, as with the other loops, can be either a single statement or a block of statements enclosed in curly braces. When a  statement is encountered, the first thing done is the initialize statement. Next, the conditional is checked. If the conditional is true, the instruction is executed, followed by the update statement, then the conditional is checked again. This accomplishes the same thing as a  statement, but allows explicit definitions of the initialization and updating of the loop, which can make the code easier to read and maintain. Thus a  statement is equivalent to initialize; while ( conditional ) {    instruction update; } but with a different presentation, one that is sometimes better as the loop controls (initialization, conditional, and update) are located next to each other in the code, allowing for easier maintenance.