Re: Just curiosity about some constructs



"Jordan Abel" <random832@xxxxxxxxx> wrote in message
news:slrndu1qar.crm.random832@xxxxxxxxxxxxxxxx
> On 2006-02-01, Rod Pemberton <dont_have@xxxxxxxxxxxxx> wrote:
> >
> > "Keith Thompson" <kst-u@xxxxxxx> wrote in message
> > news:lnacdb26n0.fsf@xxxxxxxxxxxxxxxxxx
> >> "Rod Pemberton" <dont_have@xxxxxxxxxxxxx> writes:
> >> [...]
> >> > The ultimate example of this can be found in "C: A Reference Manual"
by
> >> > Samuel Harbison and Guy Steele, Tartan Laboratories, 3rd. edition, on
> > 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);
> >> >
> >> > The only other situation where I've seen an unstructured switch was
to
> >> > select between two while's at the end of a do-while loop.  Otherwise,
> >> > structured programming of switch is the way to go (using {}).  I
think
> > the
> >> > reason behind unstructured elements in C, like 'goto' and 'switch',
was
> > to
> >>
> >> See also Duff's Device, <http://www.c-faq.com/misc/duff.html>.
> >>
> >
> > Cool!  I don't recall seeing that one, although I'm sure I read
Stroustrup's
> > book, just not as many times as Harbison's and Steele's, I guess.  But
on
> > the web, Tom Duff, regarding his "Duff's Device" said: "(Actually, I
have
> > another revolting way to use switches to implement interrupt driven
state
> > machines but it's too horrid to go into.)"  So, we are still missing the
> > _ULTIMATE_ unstructured switch.  :(  The worst (or best?) part was I
> > understood Duff's Device before reading the description.  :(
>
> Check this out:
> http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
>
> Update, 2005-03-07: Tom Duff confirms this in a blog comment. The
> "revolting way to use switches to implement interrupt driven state
> machines" of which he speaks in his original email is indeed the same
> trick as I describe here.

Yes!  Thank you.

He calls it 'continue and return,' or similar to Pascal's 'with' (I have no
idea what that means).  But, it is a nice way to 'ping-pong' between two
communicating/coordinated routines.  It essentially allows a single
procedure with one entry point to have as many entry points as needed.  Very
cool.  Usefulness, questionable...  That brings up another unstructured
element of C, besides 'goto' and 'switch', which is 'return'.  A truly
structured procedure would only have or allow a single entry point and a
single exit point, i.e., 'return'.  Instead , C allows multiple exit points,
via 'return', for procedures but has no multiple entry points by default.

Rod Pemberton


.



Relevant Pages

  • Re: PDT
    ... password to get in your switch, your entry is hashed by the same process, ... As far as the PDT password being a touchy subject... ... manuals that I received directly from Nortel that have the default password ...
    (comp.dcom.sys.nortel)
  • Re: Verifiable .exe impossible using managed C++? ERRATA to step 4.
    ... Link using switches to create a DLL, get rid of the default entry point, get rid of the C/C++ runtime, generate a relocation section, remove unused references and export your entry point ... I do not know wether it works with the -debug switch. ... Put your application object file and perhaps some other object files required ...
    (microsoft.public.dotnet.framework)
  • Re: CD left in drive repeatedly wants to install.
    ... My drive used to autoplay happily at each switch on of the computer but not kick an installation off, even after XP Home had installed. ... It started to happen after I selected to roll back the registry to what I assume to be a date prior to me having had to reinstall the software for XP for the first time. ... recently, after having backed up my registry and rolled it back from a hard reset to POST from a problem with Windows Media Player when playing a clip, a CD that I need to leave in the drive when powering on now goes into setup mode everytime I reset or switch on the machine. ... Is there a RUNONCE type registry entry that I can clear somewhere? ...
    (microsoft.public.windowsxp.newusers)
  • Re: ToC built with TC fields has unexpected formatting
    ... It's surprising in that it's being done *without* the \*mergeformat switch, ... picked up by the TOC entry, so I guess it's not surprising that TC fields ... I am intending to build my TOC ...
    (microsoft.public.word.tables)
  • Re: Coding standards and the ternary conditional operator
    ... Single exit points are a attempt to keep method logic simpler. ... Sometimes, multiple exit points make a method clearer, and sometimes they ... value based on a switch statement, e.g., ...
    (microsoft.public.dotnet.languages.csharp)