File: gawk.info, Node: For Statement, Next: Switch Statement, Prev: Do Statement, Up: Statements 7.4.4 The 'for' Statement ------------------------- The 'for' statement makes it more convenient to count iterations of a loop. The general form of the 'for' statement looks like this: for (INITIALIZATION; CONDITION; INCREMENT) BODY The INITIALIZATION, CONDITION, and INCREMENT parts are arbitrary 'awk' expressions, and BODY stands for any 'awk' statement. The 'for' statement starts by executing INITIALIZATION. Then, as long as the CONDITION is true, it repeatedly executes BODY and then INCREMENT. Typically, INITIALIZATION sets a variable to either zero or one, INCREMENT adds one to it, and CONDITION compares it against the desired number of iterations. For example: awk ' { for (i = 1; i <= 3; i++) print $i }' inventory-shipped This prints the first three fields of each input record, with one input field per output line. C and C++ programmers might expect to be able to use the comma operator to set more than one variable in the INITIALIZATION part of the 'for' loop, or to increment multiple variables in the INCREMENT part of the loop, like so: for (i = 0, j = length(a); i < j; i++, j--) ... C/C++, not awk! You cannot do this; the comma operator is not supported in 'awk'. There are workarounds, but they are nonobvious and can lead to code that is difficult to read and understand. It is best, therefore, to simply write additional initializations as separate statements preceding the 'for' loop and to place additional increment statements at the end of the loop's body. Most often, INCREMENT is an increment expression, as in the earlier example. But this is not required; it can be any expression whatsoever. For example, the following statement prints all the powers of two between 1 and 100: for (i = 1; i <= 100; i *= 2) print i If there is nothing to be done, any of the three expressions in the parentheses following the 'for' keyword may be omitted. Thus, 'for (; x > 0;)' is equivalent to 'while (x > 0)'. If the CONDITION is omitted, it is treated as true, effectively yielding an "infinite loop" (i.e., a loop that never terminates). In most cases, a 'for' loop is an abbreviation for a 'while' loop, as shown here: INITIALIZATION while (CONDITION) { BODY INCREMENT } The only exception is when the 'continue' statement (*note Continue Statement::) is used inside the loop. Changing a 'for' statement to a 'while' statement in this way can change the effect of the 'continue' statement inside the loop. The 'awk' language has a 'for' statement in addition to a 'while' statement because a 'for' loop is often both less work to type and more natural to think of. Counting the number of iterations is very common in loops. It can be easier to think of this counting as part of looping rather than as something to do inside the loop. There is an alternative version of the 'for' loop, for iterating over all the indices of an array: for (i in array) DO SOMETHING WITH array[i] *Note Scanning an Array:: for more information on this version of the 'for' loop.