Deutsch   English   Français   Italiano  
<vgaoq1$10120$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Bart <bc@freeuk.com>
Newsgroups: comp.lang.c
Subject: Re: else ladders practice
Date: Mon, 4 Nov 2024 15:21:37 +0000
Organization: A noiseless patient Spider
Lines: 99
Message-ID: <vgaoq1$10120$1@dont-email.me>
References: <3deb64c5b0ee344acd9fbaea1002baf7302c1e8f@i2pn2.org>
 <vg0t3j$2ruor$1@dont-email.me>
 <78eabb4054783e30968ae5ffafd6b4ff2e5a5f17@i2pn2.org>
 <864j4pv76h.fsf@linuxsc.com> <6726C97C.4080807@grunge.pl>
 <86ldxztzpk.fsf@linuxsc.com> <vgacoi$tr9q$1@dont-email.me>
 <6728E1F8.2090102@grunge.pl> <6728E310.2060702@grunge.pl>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 04 Nov 2024 16:21:37 +0100 (CET)
Injection-Info: dont-email.me; posting-host="b7ed5aae7ac33cde2ea3252aa4b29a5d";
	logging-data="1049664"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19UBohoElHEvAa8PA//gIOx"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Ybg7DdDWDzgUYBLAS2hQwoFy0u0=
Content-Language: en-GB
In-Reply-To: <6728E310.2060702@grunge.pl>
Bytes: 4503

On 04/11/2024 15:06, fir wrote:
> fir wrote:
>> Bart wrote:
>>> On 04/11/2024 04:00, Tim Rentsch wrote:
>>>> fir <fir@grunge.pl> writes:
>>>>
>>>>> Tim Rentsch wrote:
>>>
>>>>>> With the understanding that I am offering more than my own opinion,
>>>>>> I can say that I might use any of the patterns mentioned, depending
>>>>>> on circumstances.  I don't think any one approach is either always
>>>>>> right or always wrong.
>>>>>
>>>>> maybe, but some may heve some strong arguments (for use this and not
>>>>> that) i may overlook
>>>>
>>>> I acknowledge the point, but you haven't gotten any arguments,
>>>> only opinions.
>>>
>>> Pretty much everything about PL design is somebody's opinion.
>>
>> overally when you think and discuss such thing some conclusions may do
>> appear - and often soem do for me, though they are not always very clear
>> or 'hard'
>>
>> overally from this thread i noted that switch (which i already dont
>> liked) is bad.. note those two elements of switch it is "switch"
>> and "Case" are in weird not obvious relation in c (and what will it
>> work when you mix it etc)
>>
>> what i concluded was than if you do thing such way
>>
>>
>> a {  }  //this is analogon to case - named block
>> b {  }  //this is analogon to case - named block
>> n()   // here by "()" i noted call of some wariable that mey yeild
>> 'call' to a ,b, c, d, e, f  //(in that case na would be soem enum or
>> pointer)
>> c(  ) //this is analogon to case - named block
>> d(  ) //this is analogon to case - named block
>>
>>
>> then everything is clear - this call just selects and calls block , and
>> block itself are just definitions and are skipped in execution until
>> "called"
>>
>>
>> this is example of some conclusion for me from thsi thread - and i think
>> such codes as this my own initial example should be probably done such
>> way (though it is not c, i know
>>
>>
> note in fact both array usage like tab[5] and fuunction call like foo()
> are analogues to swich case - as when you call fuctions the call is like 
> switch and function definition sets are 'cases'
> 

Yes, switch could be implemented via a table of label pointers, but it 
needs a GNU extension.

For example this switch:

  #include <stdio.h>

  int main(void) {
    for (int i=0; i<10; ++i) {
        switch(i) {
        case 7: case 2: puts("two or seven"); break;
        case 5:         puts("five"); break;
        default:        puts("other");
        }
    }
  }


Could also be written like this:

  #include <stdio.h>

  int main(void) {
      void* table[] = {
          &&Lother, &&Lother, &&L27, &&Lother, &&Lother, &&L5,
          &&Lother, &&L27, &&Lother, &&Lother};

      for (int i=0; i<10; ++i) {
          goto *table[i];

          L27:    puts("two or seven"); goto Lend;
          L5:     puts("five"); goto Lend;
          Lother: puts("other");
          Lend:;
      }
  }

(A compiler may generate something like this, although it will be 
range-checked if need. In practice, small numbers of cases, or where the 
case values are too spread out, might be implemented as if-else chains.)