Sujet : Re: else ladders practice
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 31. Oct 2024, 19:16:23
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vg0hhn$2psrf$1@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
On 10/31/24 09:15, Anton Shepelev wrote:
fir:
somethins i got such pies 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
Why?
He has indicated that the value of n is not changed inside any of the
if-clauses. A sufficiently sophisticated compiler could notice that
fact, and also that each of the conditions is on the same variable, and
as a result it could generate the same kind of code as if it had been
written with 'else', so it won't generate unnecessary condition tests.
It might, in fact, generate the same kind of code which would have been
generated if it had been coded properly, as a switch statement, so it
might use a jump table, if appropriate.
But it's better to write it as a switch statement in the first place, so
you don't have to rely upon the compiler being sufficiently
sophisticated to get the best results.