gawk: For Statement

1 
1 7.4.4 The 'for' Statement
1 -------------------------
1 
1 The 'for' statement makes it more convenient to count iterations of a
1 loop.  The general form of the 'for' statement looks like this:
1 
1      for (INITIALIZATION; CONDITION; INCREMENT)
1        BODY
1 
1 The INITIALIZATION, CONDITION, and INCREMENT parts are arbitrary 'awk'
1 expressions, and BODY stands for any 'awk' statement.
1 
1    The 'for' statement starts by executing INITIALIZATION.  Then, as
1 long as the CONDITION is true, it repeatedly executes BODY and then
1 INCREMENT.  Typically, INITIALIZATION sets a variable to either zero or
1 one, INCREMENT adds one to it, and CONDITION compares it against the
1 desired number of iterations.  For example:
1 
1      awk '
1      {
1          for (i = 1; i <= 3; i++)
1              print $i
1      }' inventory-shipped
1 
1 This prints the first three fields of each input record, with one input
1 field per output line.
1 
1    It isn't possible to set more than one variable in the INITIALIZATION
1 part without using a multiple assignment statement such as 'x = y = 0'.
1 This makes sense only if all the initial values are equal.  (But it is
1 possible to initialize additional variables by writing their assignments
1 as separate statements preceding the 'for' loop.)
1 
1    The same is true of the INCREMENT part.  Incrementing additional
1 variables requires separate statements at the end of the loop.  The C
1 compound expression, using C's comma operator, is useful in this
1 context, but it is not supported in 'awk'.
1 
1    Most often, INCREMENT is an increment expression, as in the previous
1 example.  But this is not required; it can be any expression whatsoever.
1 For example, the following statement prints all the powers of two
1 between 1 and 100:
1 
1      for (i = 1; i <= 100; i *= 2)
1          print i
1 
1    If there is nothing to be done, any of the three expressions in the
1 parentheses following the 'for' keyword may be omitted.  Thus,
1 'for (; x > 0;)' is equivalent to 'while (x > 0)'.  If the CONDITION is
1 omitted, it is treated as true, effectively yielding an "infinite loop"
1 (i.e., a loop that never terminates).
1 
1    In most cases, a 'for' loop is an abbreviation for a 'while' loop, as
1 shown here:
1 
1      INITIALIZATION
1      while (CONDITION) {
1        BODY
1        INCREMENT
1      }
1 
11 The only exception is when the 'continue' statement (⇒Continue
 Statement) is used inside the loop.  Changing a 'for' statement to a
1 'while' statement in this way can change the effect of the 'continue'
1 statement inside the loop.
1 
1    The 'awk' language has a 'for' statement in addition to a 'while'
1 statement because a 'for' loop is often both less work to type and more
1 natural to think of.  Counting the number of iterations is very common
1 in loops.  It can be easier to think of this counting as part of looping
1 rather than as something to do inside the loop.
1 
1    There is an alternative version of the 'for' loop, for iterating over
1 all the indices of an array:
1 
1      for (i in array)
1          DO SOMETHING WITH array[i]
1 
1 ⇒Scanning an Array for more information on this version of the
1 'for' loop.
1