Path: csiph.com!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: "Rod Pemberton" Newsgroups: comp.lang.forth Subject: Re: CASE mis-understanding? Date: Sun, 09 Feb 2014 22:14:58 -0500 Organization: Aioe.org NNTP Server Lines: 108 Message-ID: References: <2014Jan27.145730@mips.complang.tuwien.ac.at> <52e7b833$0$24947$e4fe514c@dreader36.news.xs4all.nl> <52e7c363$0$24916$e4fe514c@dreader36.news.xs4all.nl> <66e2b3ae-f7c4-494a-bf1b-0a710763d8f2@googlegroups.com> <0733f401-121c-4072-8563-fd47c0d2a642@googlegroups.com> <00qdf91cor7adja0stfrn2nrdnom1hq15e@4ax.com> NNTP-Posting-Host: CNsg4fVcCsvs3UaOgZtQCw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Opera Mail/12.16 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.forth:28287 On Sun, 09 Feb 2014 00:34:20 -0500, David Thompson wrote: > On Fri, 31 Jan 2014 18:44:19 -0500, "Rod Pemberton" > wrote: >> On Fri, 31 Jan 2014 00:51:10 -0500, wrote: >> > [...] C's SWITCH works correctly. >> >> Which one? C has two: structured and unstructured. But, I'd say >> they both work correctly. >> > Huh? There's only one switch statement in C, and no other construct > implements the same semantics. It seems that the only time you post to c.l.f. is in regards to C posts but only those from me. I really don't know why you keep "attacking" me on C issues ... Do you know anything about Forth? C supports two forms of switch(). This is not explicitly spelled out in the C specifications, but it's _true_ nonetheless. It's a byproduct of how the simple language components of C work together, and work differently, when they're _not_ present. The first form is known as a structured switch. It's the form of switch() that everyone is familiar with. The switch has a body encompassed by curly braces: switch(x) { case ... : break; case ... : break; default: break; } This form of switch() can be nested with other switch()'s with bodies. It's called a structured switch because it isn't generally coded to jump into other control-flow constructs, although it can be. switch(x) { case ... : break; case 0x10 : switch(x) /* nested */ { case 0x10 : /* no conflict */ break; case ... : break; default: break; } break; default: break; } That is nestable, structured switches. The other form is the unstructured switch. It has no defined body. Unlike the structured switch(), it isn't nestable. This is because it has no body. Without a body for each switch, identical case labels would create a naming conflict. It's unstructured because it "appears" that it can jump almost anywhere due to placement of the case labels. This gives it the appearance of unstructured "spaghetti" code. There is no body to break out of, so use of "break;" should be erroneous. (The ability for a switch to jump almost anywhere in C is true for the structured switch too. Although, it's just not generally used. Duff's device is one such exception, i.e, where used.) This is the classic example of an unstructured switch(). It's from Harbison and Steele's "C: A Reference Manual", 3rd Ed. 1991, page 231: switch(x) default: if(prime(x)) case 2: case 3: case 5: case 7: process_prime(x); else case 4: case 6: case 8: case 9: case 10: process_composite(x); That is an unstructured switch. 'x' is never 0 or 1. The switch switches to the case labels for values 2 through 10, thereby bypassing _some_ of the if() logic. (I.e., 'else' must work correctly for cases 2, 3, 5, and 7, even though a conditional value for if() was never computed. It's up to the compiler as to whether 'else' is an absolute branch or a conditional branch using the conditional value for if().) The switch switches to the default label for values over 10 where the if() is executed calling prime() and executing one of the if() sections, respectively. A similar type of example is Duff's device. It uses a structured switch(), but it switches into the body of another control-flow statement, a do()-while block. (I.e., it demonstrates that a structured switch can jump anywhere the case labels are placed.) https://groups.google.com/d/msg/net.lang.c/3KFq-67DzdQ/TKl64DiBAGYJ Rod Pemberton