for Loop

From NWN Lexicon
Revision as of 22:20, 15 December 2017 by (talk) (Spam undo - Undo revision 52594 by (talk))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
// prototype
// for (expr1; expr2; expr3) { statement; }

Before executing, a for loop is initialized with a set of three expressions:

  • expr1 initializes the counter for the loop. Unlike most languages, the counter cannot be declared in the for loop.
  • expr2 is the condition for the loop's execution. If expr2 evaluates to FALSE the loop will break. This expression can be left blank to create an infinite loop if you are planning on manually breaking out of the loop using the break statement.
  • expr3 is the increment (or decrement) statement. This will be executed after all the statements in the loop code block are carried out, every time the loop is cycled through.
int i;
for (i = 0; i < 5; i++)
    // statements ...
    // this loop will cycle i as 0, 1, 2, 3, and 4

 author: Ryan Hunt, editors: Charles Feduke, Mistress, contributor: Ken Cotterill