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 indian generic levitra

generic levitra cheap

cialis tablets foreign

cheapest propecia uk

generic levitra online

online propecia prescription

ganeric cialis

brand name cialis

cheap propecia uk

get propecia online pharmacy

cheap levitra tablets

generic propecia 5mg

next day delivery cialis

overnight delivery viagra

buy generic levitra

buy propecia now

ordering propecia online

cost of propecia

buy levitra vardenafil

levitra discount

buy can from i propecia who

levitra buy online

cialis professional no prescription

generic cialis from india

canadian pharmacy viagra

lowest priced propecia

cialis cheap

cialis from canada

buying generic cialis mexico rx

buy viagra china

cialis 5 mg italia

5 mg original brand cialis

cheap levitra without prescription

buy generic viagra india rx

buy cialis in usa

levitra where to buy

generic propecia for sale

cialis to buy

lowest price on non generic levitra

low cost propecia

buying generic propecia

generic viagra made in usa

levitra canadian

buy levitra online from canada

canada meds viagra

online pharmacy propecia viagra

levitra mg

levitra next day delivery

cheapest propecia prescription

canada generic propecia

buy cialis canada

cialis vs levitra

how to get cialis in canada

indian cialis generic

daily dosage cialis

buy generic propecia

levitra online prescription

cialis price

buying cialis next day delivery

generic propecia effective

canada viagra pharmacies scam

canadian pharmacy discount code viagra

cialis 5 mg buy

canadian viagra india

buy cialis for daily use

cost levitra low

cialis and canada custom

cheap viagra canada or india

generic cialis soft tabs

cheapest overnight cialis

cheap propecia 5mg

cialis tablets

get cialis

buy cialis usa

canada viagra

best price propecia

canada cheap propecia

canadian drugs propecia

cialis discount

cialis soft pills

cialis on women

50 mg cialis

levitra viagra cialis

bestellen levitra online

cialis daily

canadian healthcare viagra

cialis overnight delivery

cheapest viagra usa

cialis price 100 mg

canada online pharmacy levitra

levitra viagra online

generic propecia online pharmacy

discount levitra purchase

info levitra

levitra online us

cheap propecia online prescription

buy levitra uk

buy cialis cannada

cialis strenght mg

buying cialis

cialis discounts

buy viagra germany canadian meds

cialis 20 mg

i need to buy propecia

generic propecia fda approved

cialis professional 100 mg

cialis dosage mg

buy cialis online canada

buy levitra online no prescription

cialis by mail

buy propecia online prescription

order viagra or levitra

discount levitra online

buy viagra online

levitra online

low cost canadian viagra

how much is viagra

how to buy cialis in canada

next day viagra

lowest price propecia

order cheap levitra

best price for propecia

how to get viagra

cheap levitra

brand viagra professional

online propecia uk

canadian online pharmacy cialis

buy cialis fedex shipping

buy canada levitra

buy propecia in the uk

buy propecia without prescription

cheap levitra prescription

mail order propecia

online generic cialis 100 mg

cheap propecia online

best price for generic cialis

cheapest price propecia cheap

lowest propecia prices

online levitra

low price levitra

levitra online no prescription

buy propecia online

online viagra gel to buy

buying levitra online

lowest price for propecia

cialis headaches

getting cialis from canada

buy levitra online viagra

cheap viagra online

genuine cialis pills

can i get viagra in mexico

buy propecia online pharmacy

how much does cialis cost

buy viagra

brand cialis for sale

order cheap propecia

cheapest prices for viagra

china viagra

canada online pharmacy propecia

best price levitra

order levitra online

cialis and diarrhea

cialis pharmacy

buy viagra on line

discount us propecia

generic viagra 100 mg

cialis for woman

buy cheap levitra

cialis fast delivery

online cialis

best way to use cialis

order propecia

get levitra

discount propecia rx

levitra cheap fast

generic levitra canada

canada levitra

indian cialis

levitra mail order

lowest propecia 1 mg

buy online prescription propecia

cialis purchase

cialis no prescription

best price cialis

buy cialis once daily

buying online propecia

how strong is 5 mg of cialis

cialis generic 100 mg

combine cialis and levitra

canadian viagra and healthcare

cheap cialis from india

buy fast propecia

cheap order prescription propecia

cialis from mexico

cheap viagra from uk

levitra online overnight delivery

cheap discount levitra

buy propecia cheap

generic cialis sale

cheap prescription propecia

buy viagra online cheap us

buy cialis without prescription

levitra tabs

canadian pharmacy cialis

how much cialis

online cheap viagra

cialis in mexico

cialis next day

levitra cost

cialis uk

bio viagra herbal

generic cialis next day shipping

canada propecia prescription

order prescription propecia

canadian healthcare

levitra prescription

indian viagra

generic viagra canada

cialis online

buy real viagra online

discount propecia online

cheapest viagra

generic viagra online

canadian healthcare pharmacy

levitra sales uk

for sale levitra

cialis buy overnight

order cheapest propecia online

cialis woman

drug generic propecia

hydrochlorothiazide cialis

herbal propecia

buy propecia online from usa pharmacy

cialis en mexico

buy cheap generic levitra

get cialis online

cialis quick shipment

discount generic propecia

generic propecia sale

levitra pill

cheap cialis

buy prescription propecia without

cheap levitra uk

buy generic cialis

cialis transdermal

buy levitra us

cialis one a day

cialis and ketoconazole

online ordering propecia

levitra online sales

cialis daily dosage pharmacy

cheapest propecia sale uk

levitra order prescription

name brand cialis

5 mg daily cialis

buy viagra mexico

brand viagra over the net

order generic levitra

discount cialis india

overnight delivery cialis

cheap canadian viagra

buying propecia

buy cheapest propecia

cheap cialis soft

online propecia prescriptions

buy cialis 5 mg

internet pharmacy propecia

cialis professional 20 mg

levitra in india

brand name cialis overnight

fda levitra

lowest propecia prices in canada

how much to buy viagra in pounds

generic levitra overnight delivery

generic levitra purchase

cost of daily cialis

gele viagra

buy propecia on line

discount drug propecia

ordering cialis gel

buy propecia generic

canada viagra generic

best price generic propecia

buy dosages levitra

buy viagra without prescription

levitra low price

once daily cialis

get levitra online

lowest price levitra

natural viagra

female viagra pills

buy real cialis

buying cialis soft tabs 100 mg

cialis 100 mg generic

obtain viagra without prescription

cheapest viagra online

levitra in canada

buy propecia where

canadian viagra

buy propecia canada