# else if statement

Page 1 of 1

## 1 Replies - 2726 Views - Last Post: 28 February 2009 - 10:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=89694&amp;s=fae1dc9fab799d871785f6ae0e12e649&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 billy81

Reputation: 0
• Posts: 31
• Joined: 24-February 09

# else if statement

Posted 28 February 2009 - 09:57 AM

hi all, else if statement works for all entered values except the one "else if (d >= 56)".
i'm not sure why it wont return the value for this else if? It should return €175.0 but it always returns €100.0
Also, could this code be more efficient somehow?
thanks and any help is appreciated.
```double fineCost = 0;		//fine
double d = a[0];				//speed
double InitialFine = 75.00;
double Deduction = 50.00;
double total = 0;

{
if (d > 35 && d <= 40)

fineCost =  InitialFine;

else if(d >= 41 && d<= 45)
{
fineCost = 25.50;
total = fineCost + InitialFine;
}
else if (d >= 46 && d <= 50)
{
fineCost = 51.0;
total = fineCost + InitialFine;
}
else if (d >= 51 && d <= 55)
{
fineCost = 76.50;
total = fineCost + InitialFine;
}
else if (d >= 56)
{
fineCost = 100.00;
total = fineCost + InitialFine;
}
else if (d < 55)
{
fineCost = total - Deduction;
}
}
System.out.println("The speeding fine is: €"+fineCost );

}

```

Is This A Good Question/Topic? 0

## Replies To: else if statement

### #2 Martyr2

• Programming Theoretician

Reputation: 5332
• Posts: 14,207
• Joined: 18-April 07

## Re: else if statement

Posted 28 February 2009 - 10:36 AM

Your ranges are just a bit mixed up and yes, there is a much more efficient way to do this. First lets describe the problem...

Your last range is if d is greater than 56 is working fine. It is what you are printing is the problem. You are not to print costFine you are to print total. costFine is 100 for if they speed greater than 56. So change the last print statement from costFine to total and you will see it is printing 175

The next thing is your last else if that reads (d < 55). This statement will only be reached if actually the value of d is below 35. That is because your first 4 else if statements are all going to catch values less than 55 (but greater than 35). So you may want to adjust that. Plus remember that in that if statement total will equal 0 so minus the deduction fineCost will come out -50. Total is set in the other else ifs. You might want to make this a separate if statement instead of an else if.

Now to make this more efficient, try a little refactoring... meaning instead of calculating the same line total = fineCost + InitialFine, try setting total to initialFine and then in each statement set fineCost. At the end add them. You can also get rid of some of these compound tests.

```if (d >= 56) { fineCost = 100.00; }
else if (d >= 51) { fineCost = 76.50; }
else if (d >= 46) { fineCost = 51.0;  }
else if (d >= 41) { fineCost = 25.50; }
else if (d > 35) { fineCost = 0; }

// Calculate here
total = InitialFine + fineCost;

```

Then all is left is to subtract out any deductions based on the speed.

Hope this helps!

"At DIC we be speed calculating code ninjas... we go 25,000 mph+ and achieve orbit!"

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }