Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!hou3c!hocda!houxm!ihnp4!inuxc!pur-ee!uiucdcs!parsec!ctvax!uokvax!emjej From: emjej@uokvax.UUCP Newsgroups: net.lang.c Subject: Re: if vs ?: - (nf) Message-ID: <5828@uiucdcs.UUCP> Date: Wed, 22-Feb-84 23:09:48 EST Article-I.D.: uiucdcs.5828 Posted: Wed Feb 22 23:09:48 1984 Date-Received: Fri, 24-Feb-84 01:22:38 EST Lines: 53 #R:iuvax:9500002:uokvax:3000019:000:1536 uokvax!emjej Feb 17 11:35:00 1984 /***** uokvax:net.lang.c / iuvax!apratt / 12:44 am Feb 15, 1984 */ .... With that in mind, can anybody tell me the likely difference between if ( i ) j++; else k++; and i ? j++ : k++ ; ...? Surely there will be differences in the compiled code.. Can somebody tell me what they are likely to be? /* ---------- */ I can only tell you what the difference would be in my code generator. The latter code (?:) would generate worse code in this context, since it looks locally like someone is going to use the results of the conditional expression. The if/else would look like cmp 0, jeq Ln inc 1,jbr L(n+1) Ln: inc 1, L(n+1): ... while the ?: would look like cmp 0, jeq Ln mov ,t0 a temporary register inc 1, jbr L(n+1) Ln: mov ,t0 inc 1, L(n+1): ... (The comparison might not be there if we knew that we'd just evaluated i so as to set flags.) All it means is that I'm not bright enough to notice that you're not using the old values of j and k for the conditional expression, and that I'm generating code for a machine whose addressing modes don't have side-effects. (It was fun to implement post-increment and decrement so as to avoid unnecessary copies, by the way--you just remember how much to tweak things just before discarding them. Watch out for base registers used to access things that you want to compare, because deferring increments on them may wipe out the condition code! In that case, just bias the offset by the increment amount and do it right away.) James Jones