🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3509

Re: Multi-level break and elimination of GOTO

#3509
From: Ray Dunn
Date: Thu, 30 Nov 1995 17:31
83 lines
3479 bytes
In referenced article I wrote...
>
>/************ Ray's named loops and switches ************/
>
>#define NamedLoop(name)   if (0) {                       \
>                            break_loop_##name: break;    \
>                            cont_loop_##name:  continue; \
>                          }
>
>#define NamedSwitch(name) if (0) {                       \
>                            break_switch_##name: break;  \
>                          } 
>
>#define BreakNamedLoop(name)    goto break_loop_##name
>#define ContinueNamedLoop(name) goto cont_loop_##name
>#define BreakNamedSwitch(name)  goto break_switch_##name
>
>/******* end named loops and switches definitions *******/

Martin D Kealey (martin@kurahaupo.gen.nz) responded by email to suggest 
that the addition of an else clause in the naming macros would improve 
them.

Thus:

>#define NamedLoop(name)   if (0) {                       \
>                            break_loop_##name: break;    \
>                            cont_loop_##name:  continue; \
>                          } else
>
>#define NamedSwitch(name) if (0) {                       \
>                            break_switch_##name: break;  \
>                          } else

which would allow us to pull the naming call out of braces and write:

  for (init; condit; step) NamedLoop(MAINLOOP)
  {
    ...

or
  switch (variable) NamedSwitch(BIGSWITCH)
  {
    ...

This is quite a nice idea which may appeal to many people's taste, 
although it does remove one advantage of the original definition, 
which is that its use is *always* benign.  The use of the "hidden" else 
clause in this alternative can have side effects not texturally 
obvious.

There are many different permutations of the macros possible, each with 
their own advantages and disadvantages and personal style issues, but 
the technique of creating unique labels and hiding the gotos under a 
structured veneer remains the same.

A further suggestion:

#define NAMED(NAME, named_construct) \
  named_construct                    \
  if (0) {                           \
    break_loop_##name: break;        \
    cont_loop_##name:  continue;     \
  } else

so that we now have:

  NAMED( MAINLOOP, while (test))

  NAMED( BIGSWITCH, switch (variable)) 

I don't like this very much because it changes the *syntax* of loops 
and switches, which the original doesn't, and if the "if" or "while" 
statements need multiple lines, you have to start using back-slashes.  
There's also too much of a temptation to put a semicolon at the end of 
those lines!

The original probably has the least disadvantages, doesn't change any 
syntax, and is the more natural to use.
-- 
Ray Dunn (opinions are my own) | Phone: (514) 938 9050
Montreal                       | Phax : (514) 938 5225
ray@ultimate-tech.com          | Home : (514) 630 3749
Technical Details
Message-ID: <49kpor$8to@ns.RezoNet.NET>
Path: arc1-ca.pugleaf.net!Rezonet.net!news
References: <43pdut$hoh@galaxy.csc.calpoly.edu> <DF8FE3.97H@news.cern.ch> <rnb68gmqlpp.fsf@euax7i3c01.eua.ericsson.se> <49ffi3$jld@ns.RezoNet.NET>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!Rezonet.net!news
From: ray@ultimate-tech.com (Ray Dunn)
Newsgroups: comp.std.c,comp.lang.c
Subject: Re: Multi-level break and elimination of GOTO
Date: 30 Nov 1995 17:31:07 GMT
Organization: Ultimate Technographics Inc.
Message-ID: <49kpor$8to@ns.RezoNet.NET>
References: <43pdut$hoh@galaxy.csc.calpoly.edu> <DF8FE3.97H@news.cern.ch> <rnb68gmqlpp.fsf@euax7i3c01.eua.ericsson.se> <49ffi3$jld@ns.RezoNet.NET>
NNTP-Posting-Host: 204.19.230.7
Mime-Version: 1.0
Content-Type: Text/Plain; charset=US-ASCII
X-Newsreader: WinVN 0.99.7