Home > empirical > To if-else-if or if-if, that is the question

To if-else-if or if-if, that is the question

August 21st, 2009 Derek-Jones

I am currently measuring if-statements, occurring in visible source, that might be mapped to an equivalent switch-statement. The most obvious usage to look for is a sequence of if-else-if statements that all involve the same expression being tested against an integer constant, as in

if (x == 1)
   stmt_1;
else
   if (x == 2)
      stmt_2;
   else
      if (x == 3)
         stmt_3;

Another possible sequence is:

if (x == 1)
   stmt_1;
if (x == 2)
   stmt_2;
if (x == 3)
   stmt_3;

provided all but the last conditionally executed arms do not change the value of the common control variable (e.g., x).

I started to wonder about what would cause a developer to chose one of these forms over the other. Perhaps the if-if form would be used when it was obvious that the common conditional variable was not modified in the conditionally executed arm. This would imply that there would be more statements in the arms of if-else-if sequences than if-if sequences. The following plot of percentage occurrence (over all detected if-else-if/if-if forms) of line number difference between pars of associated if-statements (e.g., when the controlling expression occurs on line x and the following if-statement controlling expression occurs on line x+2 the distance is 2) shows that this is not the case:

Lines between if-statement controlling expressions

Just over a quarter of the arms contain a single statement (or to be exact the code is contained on a single line); this suggests that when using the if-else-if form most developers put the else and if on the same line. At the next distance along the percentage of if-else-if forms is twice as great as the if-if, probably because of else and if appearing on separate lines (as in the introductory example) in one case and less frequently a comment/blank line in the other. Next along, why the big increase in if-if forms? A comment + blank line, or perhaps no comment or blank line but the use of curly brackets (this is too off the track of where I am supposed to be going to investigate).

This morning I realized why the original plot did not look right, one of the data sets was a way off adding to 100%. An updated version has been uploaded.

It turns out that a single statement (or at least a single line) is more common in the if-else-if form, the opposite of what I had expected. At slightly larger distances there are still differences that can be attributed to else and if appearing on separate lines, curly brackets and a comment/blank line, but the effect is not as large as seen in the original, less accurate, plot.

I have a feeling that I ought to say something about the if-else-if form being preferred to the if-if form. One of the forms will have its behavior changed if the common control variable is modified in one of its arms. But is this an intended or unintended behavior? What is the typical characteristic usage of a common control variable, e.g., do they tend to be accessed but not modified in a given function definition? At the moment I see no obvious cost or benefit strongly favoring one usage over the other, so I will remain silent on the issue.

  1. August 24th, 2009 at 05:08 | #1

    Hey Derek
    I have a doubt when you say that there is no obvious cost or benefit…If the control variable is being changed then obviously we would have to go for the 2nd form because the first wouldn’t cover all cases and if we assume that the variable is not being changed then I agree there might not be any difference in worst case scenario (last condition holds true) but wouldn’t the 2nd form cause penalties in best case scenario (first condition holds true) as many more tests would be done in 2nd case even though we don’t need to check anymore..

  2. August 24th, 2009 at 15:16 | #2

    More tests appear to be needed at runtime for the if-if usage. I say ‘appear’ because after gcc’s impressive performance on 3n+1 I thought it might spot when if-if could be implemented as if-else-if; a quick check shows that with -O2 optimization gcc does indeed implement this transformation.

  1. August 31st, 2009 at 16:06 | #1
  2. October 8th, 2009 at 19:25 | #2
Comments are closed.
www.wenn.com
FireStats icon Powered by FireStatswww.tinynibbles.com levitra discount

levitra in india

cheapest viagra online

buy real cialis

buy propecia online pharmacy

buy can from i propecia who

cheap propecia uk

buy branded viagra

levitra 10mg

order cheap levitra

low price levitra

canadian pharmacy cialis

5 mg daily cialis

cialis daily

levitra now online

generic viagra canada

levitra where to buy

cheap viagra online

brand viagra professional

cialis and canada custom

cialis online

cialis headaches

cheap levitra tablets

cialis 5 mg

buy generic propecia

generic viagra canadian

cost of propecia

buy cialis without prescription

buy cheap generic propecia

low cost canadian viagra

buy discount viagra

buy dosages levitra

buy cialis fedex shipping

order cheap propecia

name brand cialis

order viagra or levitra

generic propecia sale

cialis dosage mg

cheap levitra

cialis fast delivery

cheapest propecia uk

cost levitra low

cheapest prices for viagra

levitra online no prescription

brand name cialis overnight

canada viagra pharmacies scam

cialis next day delivery

levitra online us

ordering propecia online

online propecia uk

buy viagra online cheap us

levitra buy online

canadian drugs propecia

canada propecia prescription

buy propecia generic

mexico pharmacy cialis

cialis next day

get cialis online

discount us propecia

buy propecia on line

buying propecia

canadian online pharmacy cialis

ganeric cialis

buy canada levitra

levitra sales uk

generic levitra online

cheap cialis from india

generic viagra online

cialis 100 mg

cialis quick shipment

cialis refractory

buy cheap generic levitra

discount generic propecia

get cialis

canada viagra generic

online pharmacy propecia viagra

info levitra

buy prescription propecia without

buy propecia online prescription

generic cialis sale

lowest price levitra

levitra mail order

buy real viagra online

best price generic propecia

canada cheap propecia

low cost levitra

canadian healthcare pharmacy

cialis overnight

levitra cost

cialis 5 mg buy

online viagra gel to buy

buy cialis online uk

buy levitra uk

buy viagra

discount cialis india

get levitra online

levitra online overnight delivery

buy cialis online canada

canada online pharmacy levitra

discount levitra purchase

best price cialis

can i get viagra in mexico

low cost propecia

generic levitra vardenafil

canadian pharmacy viagra

generic propecia effective

cialis discount

cialis 20 mg

cost of daily cialis

cialis on women

once daily cialis

cheap fast levitra

lowest propecia prices in canada

buying cialis

cheap order prescription propecia

natural viagra

buy levitra online no prescription

get propecia online pharmacy

cialis from canada

cheap cialis soft

online cheap viagra

levitra mg

order generic levitra

cialis woman

cialis cheap us pharmacy

obtain viagra without prescription

buy propecia now

lowest priced propecia

brand cialis for sale

generic propecia finasteride

generic propecia online pharmacy

buy propecia where

canadian viagra and healthcare

for sale levitra

getting cialis from canada

cheapest viagra usa

cialis and ketoconazole

hydrochlorothiazide cialis

how much is viagra

mail online order propecia

cialis 50 mg

how much cialis

generic viagra made in india

online propecia prescriptions

cialis tablets foreign

online pharmacy propecia renova

cialis by mail

generic levitra cheap

generic levitra purchase

lowest propecia 1 mg

get levitra

buy propecia online from usa pharmacy

lowest price propecia best

cialis daily dosage pharmacy

cheap canadian viagra

buy cheap levitra online

cialis price 100 mg

order cheapest propecia online

lowest cost levitra

internet pharmacy propecia

cialis alternative

indian generic levitra

buy cialis once daily

cheapest propecia prescription

online cialis

buy viagra online

cialis transdermal

canada levitra

generic propecia fda approved

levitra in canada

50 mg cialis

buying generic cialis mexico rx

best viagra

buy online prescription propecia

cialis delivered overnight

combine cialis and levitra

cheap levitra prescription

cialis to buy

daily dosage cialis

cialis discounts

order prescription propecia

canadian healthcare viagra

brand viagra over the net

how to get viagra

cialis in mexico

ordering cialis gel

order levitra online

buy viagra on line

cost of viagra

cialis professional no prescription

generic viagra india

online levitra

canadian pharmacy discount code viagra

china viagra

cheap viagra canada or india

discount propecia rx

best price for generic cialis

buy propecia in the uk

brand name cialis

buy levitra online viagra

cialis for woman

levitra cheap fast

cialis cheap

levitra online prescription

cialis daily in canada

discount drug propecia

cheap propecia no prescription

5 mg original brand cialis

cialis generic 100 mg

lowest propecia prices

buy cialis 5 mg

cheap propecia online

cheapest overnight cialis

buy generic cialis

discount propecia propecia

buy propecia prescriptions online

indian cialis generic

discount propecia online

bestellen levitra online

next day delivery cialis

cialis professional 100 mg

lowest price for propecia

buy levitra online from canada

cialis from mexico

buy generic viagra india rx

cialis strenght mg

cialis price in canada

online ordering propecia

levitra for sale

cheap prescription propecia

cialis prescription

genuine cialis pills

buy cheap levitra

cheapest propecia sale uk

cheap propecia 5mg

overnight delivery viagra

buying propecia online

cheap levitra uk

buy propecia canada

cialis fast delivery usa

levitra low price

fda levitra

cialis pharmacy

buy cialis canada

overnight delivery cialis

generic cialis soft tabs

i need to buy propecia

mexico levitra

levitra next day delivery

buying cialis soft tabs 100 mg

buy generic levitra

canadian viagra 50mg

generic viagra made in usa

levitra tabs

levitra canadian

levitra viagra cialis

lowest price propecia

buy propecia without prescription

female viagra pills

canadian healthcare

generic levitra canada

generic propecia alternative

cialis 100 mg generic

buying generic propecia

how strong is 5 mg of cialis

buy propecia cheap

cialis uk

generic propecia for sale

canada meds viagra

cheap levitra without prescription

buy viagra without prescription

buy 5 mg cialis

lowest price on non generic levitra

cialis buy overnight

gele viagra

canadian viagra india

cialis vs levitra

generic viagra 100 mg

indian viagra

buy cialis cannada

buy fast propecia

buy levitra vardenafil

buying viagra in canada

canadian propecia rx

cheapest viagra

buy cialis usa

best price for propecia

buying online propecia

best price levitra

levitra prescription

buy levitra us

buy propecia online

buy cheapest propecia

cheap discount levitra

levitra online

generic levitra overnight delivery

drug generic propecia

buy viagra china

canadian viagra

cialis en mexico