🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1110

Re: Taming The Switch-Statement

#1110
From: fir
Date: Thu, 29 Oct 2026 11:31
151 lines
5499 bytes
Rosario19 pisze:
> On Mon, 26 Oct 2026 18:06:51 +0100, fir wrote:
>> bartc pisze:
>>> On 26/10/2026 16:45, fir wrote:
>>>> bartc pisze:
>>>>>> without or (or something analogic) as i understand if im not wrong
>>>>>> you cant
>>>>> You seem fixated on that 'or'. Either use 'else', or create 'or' as
>>>>> an alias as I suggested.
>>>>
>>>> imo you should try to understand the example
>>>>
>>>> for(int i = 1; i < 20; i++)
>>>> {
>>>>
>>>>           if(x%2==0) printf("\n divides by 2");
>>>>        or if(x%3==0) printf("\n divides by 3");
>>>>        else  printf("\n no divides (by 2 or 3)");
>>>> }
>>>>
>>>> you understand how this would work? i think you do
>>>>
>>>> and how in normal c without this "or" it would look,
>>>> for exampel in your if else model?
>>>
>>> Show me exactly what the output should be, when it is written like this:
>>>
>>>             if(x%2==0) printf("\n%d divides by 2", i);
>>>          or if(x%3==0) printf("\n%d divides by 3", i);
>>>          else  printf("\n%d no divides (by 2 or 3)", i);
>>>
>>> You might also want to look at the FizzBuzz program.
>>>
>> 1 no divides (by 2 or 3)
>> 2 divides by 2
>> 3 divides by 3
>> 4 divides by 2
>> 5 no divides (by 2 or 3)
>> 6 divides by 2
>> 6 divides by 3
>> 7 no divides (by 2 or 3)
>> 8 divides by 2
>> 9 divides by 3
>> 10 divides by 2
>> 11 no divides (by 2 or 3)
>> 12 divides by 2
>> 12 divides by 3
>> 13 no divides (by 2 or 3)
>> 14 divides by 2
>> 15 divides by 3
>> 16 divides by 2
>> 17 no divides (by 2 or 3)
>> 18 divides by 2
>> 18 divides by 3
>> 19 no divides (by 2 or 3)
> 
> for(x=1;x<20;++x)
>    {if(x%2==0) printf("%d divides by 2\n",x);
>     if(x%3==0) printf("%d divides by 3\n",x);
>     if(x%3!=0&&x%2!=0)
>             printf("%d no divides (by 3 or 2)\n",x);
>    }
> 

explicitely repeating calculation is not a good thing here

introducing separate soecial variable to handle this also not



normally you would need something like this  above and this onei guess



  for(x=1;x<20;++x)
     {
     int taken =0;
      if(x%2==0)taken++, printf("%d divides by 2\n",x);
      if(x%3==0) taken++, printf("%d divides by 3\n",x);
      if(!taken)
              printf("%d no divides (by 3 or 2)\n",x);
     }


but both are kinda violations

where

  for(x=1;x<20;++x)
     {

      if(x%2==0) printf("%d divides by 2\n",x);
      or if(x%3==0) printf("%d divides by 3\n",x);
      else    printf("%d no divides (by 3 or 2)\n",x);
     }

is not

(and i think there are more such combination you cannot expres
with if else , though i not checked for example if

for(x=1;x<20;++x)
     {

      if(x%2==0) printf("%d divides by 2\n",x);
      and if(x%3==0) printf("%d divides by 3\n",x);
      else    printf("%d no divides (by both 3 and 2)\n",x);
     }

this you can express (seems also no as the both %2 %3 you need to go 
thru so you cant goto  and if you go thru you need to remember the states


%2 else goto B
// %2 tru
%3 else goto D
//%2%3 tru

D
%2 tru
%3 false

B: //  %2 fale
%3 else goto C
//%2 false %3 true here


C
//%2%3 false here


so in fact you CAN unwind this

    if(x%2==0) printf("%d divides by 2\n",x); else goto mod2false;
     if(x%3==0) printf("%d divides by 3\n",x); else goto mod2true3false;
     goto out;
mod2true3false:
    goto out;

mod2false:
    if(x%3==0) printf("%d divides by 3\n",x); else mod2false3false;
   goto out;
mod2false3false:
     printf("%d no divides (by both 3 and 2)\n",x);
out:

but its also bad - you repeat the mod 3 calculation in 2 branches

on assembly level it seem you also need to hold a state but its inner 
state can be uin register you dont need name it etc
Technical Details
Message-ID: <11bvar6$39oar$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <119qf6l$2rmg3$1@dont-email.me> <11ba02r$3pl5h$1@dont-email.me> <11bnfpm$i84l$1@dont-email.me> <11bng23$iblc$1@dont-email.me> <11bngj1$ifj5$1@dont-email.me> <11bnnbg$l7i1$1@dont-email.me> <11bntk0$nsbo$1@dont-email.me> <11bnvbq$olbj$1@dont-email.me> <11bo02r$otr5$1@dont-email.me> <11bo0ul$pbso$1@dont-email.me> <11bo1bc$phlr$1@dont-email.me> <tft5elp1q2ns7rhfp3rod862ks5ip3p0ms@4ax.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: fir <profesor.fir@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: Taming The Switch-Statement
Date: Thu, 29 Oct 2026 12:31:48 +0100
Organization: A noiseless patient Spider
Lines: 151
Message-ID: <11bvar6$39oar$1@dont-email.me>
References: <119qf6l$2rmg3$1@dont-email.me> <11ba02r$3pl5h$1@dont-email.me>
 <11bnfpm$i84l$1@dont-email.me> <11bng23$iblc$1@dont-email.me>
 <11bngj1$ifj5$1@dont-email.me> <11bnnbg$l7i1$1@dont-email.me>
 <11bntk0$nsbo$1@dont-email.me> <11bnvbq$olbj$1@dont-email.me>
 <11bo02r$otr5$1@dont-email.me> <11bo0ul$pbso$1@dont-email.me>
 <11bo1bc$phlr$1@dont-email.me> <tft5elp1q2ns7rhfp3rod862ks5ip3p0ms@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 29 Oct 2026 11:31:51 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3465563"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+gAHm8iUh+H/Q+ILJqlGLH8R3xHsvp3pg="; posting-host="411f01e87900b90aa8c27c0bfeeb5517"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101
 Firefox/140.0 SeaMonkey/2.53.24
Cancel-Lock: sha1:j+bPmpxg9D12Gy7uIvRESDuqEp4= sha256:FkpUm0Aim2NkiBe4+8q7xmnPKpI9rw77xDZlvgTFzOk=
	sha1:+73D2PPKlJWtRk7nNpcRnYAIzvg= sha256:l+B7XPFo0AA8Us/ljghQzQUewIbYTsSWbUlLw+d8TD8=
In-Reply-To: <tft5elp1q2ns7rhfp3rod862ks5ip3p0ms@4ax.com>