Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

About the control statement 'do statement while (condition)' in gawk.

40 views
Skip to first unread message

Hongyi Zhao

unread,
Oct 14, 2016, 7:56:15 PM10/14/16
to
Hi all,

In the gawk manual page, I can find the descriptions on the control
statements "do statement while (condition)".

But, it's well known that gawk has the classical '/pattern/ {action}'
construction. But, as you can see, in the 'do statement while
(condition)' control statement, it basically is a '{action} /pattern/'
construction.

So, I just want to know why gawk designs this control statement like
thins form instead of the form like 'while (condition) do statement'
which is similar to the form used in the bash?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Kaz Kylheku

unread,
Oct 14, 2016, 8:57:56 PM10/14/16
to
On 2016-10-14, Hongyi Zhao <hongy...@gmail.com> wrote:
> Hi all,
>
> In the gawk manual page, I can find the descriptions on the control
> statements "do statement while (condition)".
>
> But, it's well known that gawk has the classical '/pattern/ {action}'
> construction. But, as you can see, in the 'do statement while
> (condition)' control statement, it basically is a '{action} /pattern/'
> construction.

What you probably don't know is that "do statement while (condition)"
is straight out of the C language. The Awk grammar is strongly
influenced by C, the language in which Awk was developed (and its
clones still are, which is besides the point).

>
> So, I just want to know why gawk designs this control statement like
> thins form instead of the form like 'while (condition) do statement'
> which is similar to the form used in the bash?

Somehow you missed that awk has "while (condition) statement" too; also
straight out of the C language. Of course, statement can be a
brace-enclosed compound:

$ awk 'BEGIN { while (x < 10) print x++; }'
0
1
2
3
4
5
6
7
8
9

The Bourne shell's while commands .. ; do commands ...; done may be
inspired by Algol, like many of the Bourne shell features.
The original Bourne shell was written in C, using macros to make it
look like Algol.

Here are the macros (1979 dated V7 Unix!)

http://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh/mac.h

Then in the directory you can browse for various .c files that
all use these macros with an obsessive consistency:

http://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/sh

For instance, while loops are coded as

WHILE condition DO .... OD

or

WHILE condition-with-side-effect DONE

The shell has similar syntactic elements.

Janis Papanagnou

unread,
Oct 14, 2016, 10:06:45 PM10/14/16
to
On 15.10.2016 01:56, Hongyi Zhao wrote:
> Hi all,
>
> In the gawk manual page, I can find the descriptions on the control
> statements "do statement while (condition)".
>
> But, it's well known that gawk has the classical '/pattern/ {action}'
> construction. But, as you can see, in the 'do statement while
> (condition)' control statement, it basically is a '{action} /pattern/'
> construction.

I think you are here mixing up the awk'ish condition/action syntax with
the imperative statements that you find in awk's action part.

These two versions of awk's while-loops may appear _in the action part_

while (condition) statement

and

do statement ; while condition

(besides all the other imperative commands, like if, case, etc.).

>
> So, I just want to know why gawk designs this control statement like
> thins form instead of the form like 'while (condition) do statement'
> which is similar to the form used in the bash?

Programming language designers are free to define the supported forms
of conditional loops. One set of typical loops are classified by two
factors; whether the condition is checked on extry (i.e. before the
statement is exectuted), or checked on exit (after the first execution
of the statement), and in either positive or negated form. Informally
(keywords and syntax may differ depending on the language):

1. while cond do stmt
2. until cond do stmt
3. do stmt while cond
4. do stmt until cond

The shells (ksh, bash) support two forms, 1. and 2., awk and C supports
1. and 3., other languages support differnt forms, e.g. Pascal 1. and
4., Algol68 (in other design respects the prototype of shell design)
supports only 1., but the while loop in Algol is part of a much more
powerful loop construct (for ... while ... do ... od, with almost all
parts optional, depending on the desired semantics), and that you can
also find in Simula67 for example. Ignoring the more powerful Algol/
Simula loop you typically don't need all four loop variants since you
have usually the negation operator available that allows the designer
to support just 1. or 2. and 3. or 4. (The fact that shell supports 1.
and 2. results in more clumsy code to implement 3. or 4. which cannot
be simply emulated using the negation operator.)

Janis

>
> Regards
>

Janis Papanagnou

unread,
Oct 30, 2016, 11:24:37 AM10/30/16
to
This is an addendum to the [off-topic] shell (bash) related reference
in the original question. In the explanations that I gave, as it just
occurred to me, one statement requires a correction.

(Followup-to: comp.unix.shell)

On 15.10.2016 04:06, Janis Papanagnou wrote:
> On 15.10.2016 01:56, Hongyi Zhao wrote:
[...]
Actually, the last statement in parenthesis is not appropriate for shell;
and the reason is exactly the Algol68 based concepts that apply here. In
shell it is possible to support cases 3. and 4. without duplication of
code or need to write a function to avoid such duplication. The patterns
for those cases can simply be written as

3a. while stmt ; cond ; do : ; done
4a. until stmt ; cond ; do : ; done

It's generally possible to formulate more general loops like (informally)

loop stmt1 ; exit on cond ; stmt2 ; endloop

that way by replacing the colon (true command) appropriately.

5. until stmt1 ; cond ; do stmt2 ; done


Janis

0 new messages