Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #10012

GAWK mystery parses

Path csiph.com!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups comp.lang.awk
Subject GAWK mystery parses
Date Fri, 3 Oct 2025 04:36:17 -0000 (UTC)
Organization The official candy of the new Millennium
Message-ID <10bnjs1$br9v$1@news.xmission.com> (permalink)
Injection-Date Fri, 3 Oct 2025 04:36:17 -0000 (UTC)
Injection-Info news.xmission.com; posting-host="shell.xmission.com:166.70.8.4"; logging-data="388415"; mail-complaints-to="abuse@xmission.com"
X-Newsreader trn 4.0-test77 (Sep 1, 2010)
Originator gazelle@shell.xmission.com (Kenny McCormack)
Xref csiph.com comp.lang.awk:10012

Show key headers only | View raw


The topic for today is: What can we get away with for clause 3 of a "for"
statement.

Observe (notice that the print statement is *inside* the for statement):

First, we use a print statement as clause 3:
 % gawk4 'BEGIN { for (i=1; i<=5; print "i =",i++); }'
i = 1
i = 2
i = 3
i = 4
i = 5

But here we show that print really is a statement and not an expression:
 % gawk4 'BEGIN { print (print "help") }'
gawk4: cmd. line:1: BEGIN { print (print "help") }
gawk4: cmd. line:1:               ^ syntax error
 %
(You get the same error with or without the parens)

Now, the point here is that I always thought that clause 3 in a for
statement had to be an expression (like clauses 1 and 2).  But it seems we
can get away with having a statement there - if it is simple enough.

Note that in C, there's no real distinction between a simple statement and
an expression; for example, printf() is usually used as if it was a
statement, but it is in fact a function that returns a value, i.e., an
expression.  So, in C, clause 3 of the for statement is usually thought of
as a statement, but is, in fact, an expression.

Next, we press our luck:

This works:
 % gawk4 'BEGIN { for (i=1; i<=5;) if (i++ == 3) print "three" }'
three
 %

But this doesn't:
 % gawk4 'BEGIN { for (i=1; i<=5; if (i++ == 3) print "three"); }'
gawk4: cmd. line:1: BEGIN { for (i=1; i<=5; if (i++ == 3) print "three"); }
gawk4: cmd. line:1:                         ^ syntax error
gawk4: cmd. line:1: BEGIN { for (i=1; i<=5; if (i++ == 3) print "three"); }
gawk4: cmd. line:1:                                                    ^ syntax error
 %

Why not?

-- 
You are again heaping damnation upon your own head by your statements.

    - Rick C Hodgin -

Back to comp.lang.awk | Previous | NextNext in thread | Find similar


Thread

GAWK mystery parses gazelle@shell.xmission.com (Kenny McCormack) - 2025-10-03 04:36 +0000
  Re: GAWK mystery parses Kaz Kylheku <643-408-1753@kylheku.com> - 2025-10-03 06:20 +0000
    Re: GAWK mystery parses gazelle@shell.xmission.com (Kenny McCormack) - 2025-10-03 07:06 +0000

csiph-web