Article View: comp.std.c
Article #4319Re: Grammar for Switch Statement
From: Paul Long
Date: Thu, 02 Nov 1995 01:43
Date: Thu, 02 Nov 1995 01:43
50 lines
2872 bytes
2872 bytes
diamond@jrd.dec.com (Norman Diamond) wrote:
[snip]
>>In article <470ih7$mvh@gaia.ns.utk.edu>,
>>Eric Lemings <elemings@unix1.utm.edu> wrote:
>>>Why is the grammar specification for a 'switch' statement:
>>> switch ( expression ) statement
>>>instead of:
>>> switch ( expression ) block
[snip]
>Next question is why they require parentheses...
I see what you mean--switch doesn't really need parentheses.
(That is, if we keep the case keyword. For example,
"switch 5+2 case -3:..." is okay, whereas "switch 5+2 -3:..."
is ambiguous.) Maybe it was done to make it look like the
other control statements, which need parentheses (for example,
"if a + 3;" would be ambiguous). I'll bet that's it.
>...when return and sizeof don't
>require parentheses around the expression.
This is off-topic (not Standard-C related), but why do most
programmers use parentheses around the value in a return
statement? They are absolutely not needed, and I think it looks a
lot cleaner without them. A colleague of mine said that she
stopped using them after her "retrun(3);" typo wasn't caught until
the linker--it looked like a function call to the compiler. Is
this an inherited idiom from BASIC syntax or somesuch that has
been propagated through the millenia by unquestioning comp-sci
instructors and books on C programming? Some writer/programmers,
who I'm sure know better, such as Plauger, have this habit.
Oh, and sizeof requires parentheses if its operand is a type name.
This is to prevent syntactical ambiguity in situations such as
"sizeof int * + 3". Is this "sizeof(int) * +3" or
"sizeof(int *) + 3". Most people just always enclose the sizeof
operand with parentheses so they don't have to think about it.
>Next, why did they spell out
>the word in full, and why did they waste two keywords (swit and case) when
>they could have done it in one.
Are you saying this tongue-in-cheek? If not, see the ambiguous
case-less "case," above. :-)
--
Paul Long 45:29:14N 122:48:09W
plong@perf.com http://www.teleport.com/~pciwww/
"Where time or intelligence are lacking, a goto may do the job."
- M.E. Hopkins, "A Case for the GOTO," 1972.
Technical Details
Message-ID:
<4797ni$avq@news0.rain.rg.net>Path:
arc1-ca.pugleaf.net!usenet.eel.ufl.edu!pacifier!rainrgnews0!usenetReferences:
<470ih7$mvh@gaia.ns.utk.edu> <4713rc$3d9@solutions.solon.com> <476n45$8g0@jrdgwm.jrd.dec.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!usenet.eel.ufl.edu!pacifier!rainrgnews0!usenet From: Paul Long <plong@perf.com> Newsgroups: comp.std.c Subject: Re: Grammar for Switch Statement Date: 2 Nov 1995 01:43:14 GMT Organization: Performance Computing Incorporated Message-ID: <4797ni$avq@news0.rain.rg.net> References: <470ih7$mvh@gaia.ns.utk.edu> <4713rc$3d9@solutions.solon.com> <476n45$8g0@jrdgwm.jrd.dec.com> NNTP-Posting-Host: yosemite.perf.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.1 sun4c) X-URL: news:476n45$8g0@jrdgwm.jrd.dec.com