Re: compiler question
- From: Roedy Green <see_website@xxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 29 Jun 2008 19:45:07 GMT
Sun, 29 Jun 2008 11:25:56 -0700 (PDT), ankur
<ankur.a.agarwal@xxxxxxxxx> wrote, quoted or indirectly quoted someone
who said :
if (weight < 10) thePrice = 1000;
if (weight > 50) thePrice = 5000;
if (weight >= 10) thePrice = weight*10; // Always
executed.
System.out.println("The price is: " + thePrice); // (1)
You are presuming greater intelligence of the compiler than it has.
Is sees this blurrily something ilke
if ( somethingoroother ) assign price
if ( somethingelse ) assign price
if (yetanothingh thing) assign price
Had you written this as:
if (weight < 10)
{
thePrice = 1000;
}
else if (weight > 50)
{
thePrice = 5000;
}
else
{
thePrice = weight*10;
}
Then it could be absolutely sure thePrice will be assigned. It does
not have to analyse the conditional expressions to know this.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
.
- Follow-Ups:
- Re: compiler question
- From: Maarten Bodewes
- Re: compiler question
- References:
- compiler question
- From: ankur
- compiler question
- Prev by Date: Re: Controlling the scroll pane in JComboBox
- Next by Date: Re: Recommend: Java based CMS
- Previous by thread: Re: compiler question
- Next by thread: Re: compiler question
- Index(es):
Relevant Pages
|