awk:awk_loops
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
awk:awk_loops [2020/05/06 19:53] – peter | awk:awk_loops [2020/07/15 09:30] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 9: | Line 9: | ||
===== For loops ===== | ===== For loops ===== | ||
- | The **for** statement performs some initialization action, then it checks the condition. | + | A **for** statement performs some initialization action, then it checks the condition. |
If the condition is true, it executes actions, thereafter it performs increment or decrement operation. | If the condition is true, it executes actions, thereafter it performs increment or decrement operation. | ||
Line 33: | Line 33: | ||
===== While Loop ===== | ===== While Loop ===== | ||
- | The while loop keeps executing the action until a particular logical condition evaluates to true. | + | A **while** loop keeps executing the action until a particular logical condition evaluates to true. |
AWK first checks the condition; if the condition is true, it executes the action. | AWK first checks the condition; if the condition is true, it executes the action. | ||
Line 39: | Line 39: | ||
This process repeats as long as the loop condition evaluates to true. | This process repeats as long as the loop condition evaluates to true. | ||
- | < | + | < |
awk 'BEGIN {i = 1; while (i < 6) { print i; ++i } }' | awk 'BEGIN {i = 1; while (i < 6) { print i; ++i } }' | ||
</ | </ | ||
Line 57: | Line 57: | ||
===== Do-While Loop ===== | ===== Do-While Loop ===== | ||
- | The **do-while** loop is similar to the while loop, except that the test condition is evaluated at the end of the loop. | + | A **do-while** loop is similar to the while loop, except that the test condition is evaluated at the end of the loop. |
In a do-while loop, the action statement gets executed at least once even when the condition statement evaluates to false. | In a do-while loop, the action statement gets executed at least once even when the condition statement evaluates to false. | ||
- | < | + | < |
awk 'BEGIN {i = 1; do { print i; ++i } while (i < 6) }' | awk 'BEGIN {i = 1; do { print i; ++i } while (i < 6) }' | ||
</ | </ | ||
Line 79: | Line 79: | ||
===== Break Statement ===== | ===== Break Statement ===== | ||
- | A Break is used to end the loop execution. | + | **Break** is used to end the loop execution. |
- | < | + | < |
awk 'BEGIN { | awk 'BEGIN { | ||
sum = 0; for (i = 0; i < 20; ++i) { | sum = 0; for (i = 0; i < 20; ++i) { | ||
Line 108: | Line 108: | ||
===== Continue Statement ===== | ===== Continue Statement ===== | ||
- | The **continue** statement is used inside a loop to skip to the next iteration of the loop. | + | A **continue** statement is used inside a loop to skip to the next iteration of the loop. |
It is useful when you wish to skip the processing of some data inside the loop. | It is useful when you wish to skip the processing of some data inside the loop. | ||
- | < | + | < |
awk 'BEGIN { | awk 'BEGIN { | ||
for (i = 1; i <= 20; ++i) { | for (i = 1; i <= 20; ++i) { | ||
Line 145: | Line 145: | ||
If no argument is supplied, exit returns a zero. | If no argument is supplied, exit returns a zero. | ||
- | < | + | < |
awk 'BEGIN { | awk 'BEGIN { | ||
sum = 0; for (i = 0; i < 20; ++i) { | sum = 0; for (i = 0; i < 20; ++i) { |
awk/awk_loops.1588794800.txt.gz · Last modified: 2020/07/15 09:30 (external edit)