Deutsch   English   Français   Italiano  
<4f6092eae7e1ae78291c381250e6f353ba1fb65b@i2pn2.org>

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

Path: ...!weretis.net!feeder9.news.weretis.net!news.nk.ca!rocksolid2!i2pn2.org!.POSTED!not-for-mail
From: fir <fir@grunge.pl>
Newsgroups: comp.lang.c
Subject: Re: else ladders practice
Date: Thu, 31 Oct 2024 15:49:11 +0100
Organization: i2pn2 (i2pn.org)
Message-ID: <4f6092eae7e1ae78291c381250e6f353ba1fb65b@i2pn2.org>
References: <3deb64c5b0ee344acd9fbaea1002baf7302c1e8f@i2pn2.org> <slrnvi746p.fkp.dan@djph.net> <6723944E.2040305@grunge.pl> <slrnvi75c6.fkp.dan@djph.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 31 Oct 2024 14:49:15 -0000 (UTC)
Injection-Info: i2pn2.org;
	logging-data="310548"; mail-complaints-to="usenet@i2pn2.org";
	posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0";
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
In-Reply-To: <slrnvi75c6.fkp.dan@djph.net>
X-Spam-Checker-Version: SpamAssassin 4.0.0
Bytes: 3827
Lines: 84

Dan Purgert wrote:
> On 2024-10-31, fir wrote:
>> Dan Purgert wrote:
>>> On 2024-10-31, fir wrote:
>>>> somethins i got such pices of code like
>>>>
>>>> if(n==1) {/*something/}
>>>> if(n==2) {/*something/}
>>>> if(n==3) {/*something/}
>>>> if(n==4) {/*something/}
>>>> if(n==5) {/*something/}
>>>>
>>>> technically i would need to add elses - but the question is if to do that
>>>>
>>>> do teh code has really chance to be slower without else (except some
>>>> very prmitive compilers) ??
>>>
>>> In the above, all conditionals are always checked -- that is the truth
>>> of a previous conditional statement has no bearing on subsequent tests.
>>> This leads to the potential of tests going off in directions you hadn't
>>> necessarily anticipated.
>>>
>>> However, 'if..elseif..else' will only check subsequent conditionals if
>>> the prior statements were false.  So for the case that "n=2", you're
>>> only ever testing the two cases "if (n==1)" (which is false) and
>>> "elseif(n==2)".  The computer just skips to the end of the set of
>>> statements.
>>>
>>>
>>> Given this MWE (my own terrible code aside ;) ):
>>>
>>>     int main(){
>>>       int n=0;
>>>       printf ("all if, n=%u\n",n);
>>>       if (n==0) { printf ("n: %u\n",n); n++;}
>>>       if (n==1) { printf ("n: %u\n",n); n++;}
>>>       if (n==2) { printf ("n: %u\n",n); n++;}
>>>       if (n==3) { printf ("n: %u\n",n); n++;}
>>>       if (n==4) { printf ("n: %u\n",n); n++;}
>>>       printf ("all if completed, n=%u\n",n);
>>>
>>>       n=3;
>>>       printf ("with else if, n=%u\n",n);
>>>       if (n==0) { printf ("n: %u\n",n); n++;}
>>>       else if (n==1) { printf ("n: %u\n",n); n++;}
>>>       else if (n==2) { printf ("n: %u\n",n); n++;}
>>>       else if (n==3) { printf ("n: %u\n",n); n++;}
>>>       else { printf ("n: %u\n",n); n++;}
>>>       printf ("with else if completed, n=%u\n",n);
>>>     }
>>>
>>>
>>> You'll get the output:
>>>
>>> all if, n=0
>>> n: 0
>>> n: 1
>>> n: 2
>>> n: 3
>>> n: 4
>>> all if completed, n=5
>>> with else if, n=3
>>> n: 3
>>> with else if completed, n=4
>>>
>>> HTH :)
>>>
>> i not modify n in those {} blocks so this example is not much relevant
>
> I'm using that as a simplified case to force the issue.  "n" could be
> modified anywhere, just so long as it is "between" any two of the test
> cases being checked.
>

bot not in the cases i got on mind  (i wrote its if elseelse else elese
case but with else just skipped

probbaly people use switch in such case but i distasted switch so i
iused else ladders, now i think if to use just ifs
>>
>> my quiestion is more liek what is a metter of beter style
>
> If it is a series of related conditions, then "if .. else if .. else".
>