21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

106 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>}Control flow in the switch statement works as it does in C. Once a match to a givencase is made, case statement bodies are executed until a break, continue, next, nextfileor exit is encountered, or the end of the switch statement itself. For example:switch (NR * 2 + 1) {case 3:case "11":print NR - 1breakcase /2[[:digit:]]+/:print NRdefault:print NR + 1case -1:print NR * -1}Note that if none of the statements specified above halt execution of a matched casestatement, execution falls through to the next case until execution halts. In the aboveexample, for any case value starting with ‘2’ followed by one or more digits, the printstatement is executed and then falls through into the default section, executing its printstatement. In turn, the −1 case will also be executed since the default does not haltexecution.6.4.6 The break StatementThe break statement jumps out of the innermost for, while, or do loop that encloses it.The following example finds the smallest divisor of any integer, and also identifies primenumbers:# find smallest divisor of num{num = $1for (div = 2; div*div

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!