<h1 id="gnu-awk---part-8">Gnu Awk - Part 8</h1>
<h2 id="introduction">Introduction</h2>
<p>This is the eighth episode of the &quot;<a href="https://hackerpublicradio.org/series/0094.html" title="Learning Awk">Learning Awk</a>&quot; series that
<a href="https://hackerpublicradio.org/correspondents/0300.html" title="b-yeezi">b-yeezi</a> and I are doing.</p>
<h2 id="recap-of-the-last-episode">Recap of the last episode</h2>
<ul>
<li><p>The <code>while</code> loop: tests a condition and performs commands <em>while</em> the test returns true</p></li>
<li><p>The <code>do while</code> loop: performs commands after the <code>do</code>, then tests afterwards, repeating the commands <em>while</em> the test is true.</
p></li>
<li><p>The <code>for</code> loop (type 1): initialises a variable, performs a test, and increments the variable all together, performing commands while the test is 
true.</p></li>
<li><p>The <code>for</code> loop (type 2): sets a variable to successive indices of an array, preforming a collection of commands for each index.</p></li>
</ul>
<p>These types of loops were demonstrated by examples in the <a href="https://hackerpublicradio.org/eps.php?id=2330" title="Awk Part 7">last episode</a>.</p>
<p>Note that the example for '<code>do while</code>' was an infinite loop (perhaps as a test of the alertness of the audience!):</p>
<pre><code>#!/usr/bin/awk -f
BEGIN {

    i=2;
    do {
        print &quot;The square of &quot;, i, &quot; is &quot;, i*i;
        i = i + 1
    }
    while (i != 2)

exit;
}</code></pre>
<p>The condition in the <code>while</code> is always true:</p>
<pre><code>The square of  2  is  4
The square of  3  is  9
The square of  4  is  16
The square of  5  is  25
The square of  6  is  36
The square of  7  is  49
The square of  8  is  64
The square of  9  is  81
The square of  10  is  100
...
The square of  1269630  is  1611960336900
The square of  1269631  is  1611962876161
The square of  1269632  is  1611965415424
The square of  1269633  is  1611967954689
The square of  1269634  is  1611970493956
...</code></pre>
<p>The variable <code>i</code> is set to 2, the <code>print</code> is executed, then <code>i</code> is set to 3. The test &quot;<code>i != 2</code>&quot; is true and will be <em>ad infinitum</em>.</p>
<h2 id="some-more-statements">Some more statements</h2>
<p>We will come back to loops later in this episode, but first this seems like a good point to describe another statement: the <code>switch</code> statement.</p>
<h2 id="long-notes">Long notes</h2>
<p>The notes for rest of this episode are available <a href="https://hackerpublicradio.org/eps/hpr2438/full_shownotes.html">here</a>.</p>
<h2 id="links">Links</h2>
<ul>
<li><a href="https://www.gnu.org/software/gawk/manual/html_node/index.html"><em>GNU Awk User's Guide</em></a></li>
<li>Previous shows in this series on HPR:
<ul>
<li><a href="https://hackerpublicradio.org/eps.php?id=2114">&quot;<em>Gnu Awk - Part 1</em>&quot;</a> - episode 2114</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2129">&quot;<em>Gnu Awk - Part 2</em>&quot;</a> - episode 2129</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2143">&quot;<em>Gnu Awk - Part 3</em>&quot;</a> - episode 2143</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2163">&quot;<em>Gnu Awk - Part 4</em>&quot;</a> - episode 2163</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2184">&quot;<em>Gnu Awk - Part 5</em>&quot;</a> - episode 2184</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2238">&quot;<em>Gnu Awk - Part 6</em>&quot;</a> - episode 2238</li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2330">&quot;<em>Gnu Awk - Part 7</em>&quot;</a> - episode 2330</li>
</ul></li>
<li>Resources:
<ul>
<li><a href="https://hackerpublicradio.org/eps/hpr2438/full_shownotes.epub">ePub version of these notes</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr2438/full_shownotes.pdf">PDF version of these notes</a></li>
<li>Demonstration of the <code>switch</code> statement:
<ul>
<li>Script: <a href="https://hackerpublicradio.org/eps/hpr2438/switch_example.awk">switch_example.awk</a></li>
<li>Output: <a href="https://hackerpublicradio.org/eps/hpr2438/switch_example.out">switch_example.out</a></li>
</ul></li>
<li>Demonstration of the <code>break</code> statement:
<ul>
<li>Script: <a href="https://hackerpublicradio.org/eps/hpr2438/divisor.awk">divisor.awk</a></li>
<li>Output: <a href="https://hackerpublicradio.org/eps/hpr2438/divisor.out">divisor.out</a></li>
</ul></li>
<li>Demonstration of the <code>continue</code> statement:
<ul>
<li>Script: <a href="https://hackerpublicradio.org/eps/hpr2438/continue_example.awk">continue_example.awk</a></li>
</ul></li>
<li>Demonstration of the <code>next</code> statement:
<ul>
<li>Script: <a href="https://hackerpublicradio.org/eps/hpr2438/next_example.awk">next_example.awk</a></li>
<li>Output: <a href="https://hackerpublicradio.org/eps/hpr2438/next_example.out">next_example.out</a></li>
</ul></li>
</ul></li>
</ul>
