Ankur Sharma
Ankur Sharma

Reputation: 283

Java the for loop continues to execute even after the condition is fulfilled

I was running this code and the loop didn't seem to stop at the specified condition.

for(double i=1.0; i!=2.0; i+=0.2){
    System.out.println("The value of i :" +i);
}

Is this a problem with the way the double number is represented in Java?

Upvotes: 2

Views: 103

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726549

This is a fairly common problem: 0.2 cannot be represented exactly in a double, so 2.0 is not equal to the sum of ten 0.2 added together. You need to use < instead of != to stop the loop.

Note that your loop would have worked for an increment which is equal to a negative power of 2, say, 0.25: the loop below works fine, and stops as expected:

for(double i=1.0; i!=2.0; i+=0.25){
    System.out.println("The value of i :" +i);
}

This works, because 0.25 can be represented exactly in a double. This little experiment shows that you need to be extremely cautious when comparing doubles for equality or inequality.

Upvotes: 7

assylias
assylias

Reputation: 328598

Yes that's it - 0.2 can't be exactly represented as a double. To see the exact values, you can run the following program:

public static void main(String[] args) {
    for (double i = 1.0; i != 2.0 && i < 3.0; i += 0.2) {
        System.out.println("The value of i :" + new BigDecimal(i));
    }
}

which prints:

The value of i :1
The value of i :1.1999999999999999555910790149937383830547332763671875
The value of i :1.399999999999999911182158029987476766109466552734375
The value of i :1.5999999999999998667732370449812151491641998291015625
The value of i :1.79999999999999982236431605997495353221893310546875
The value of i :1.9999999999999997779553950749686919152736663818359375
The value of i :2.199999999999999733546474089962430298328399658203125
The value of i :2.399999999999999911182158029987476766109466552734375
The value of i :2.600000000000000088817841970012523233890533447265625
The value of i :2.800000000000000266453525910037569701671600341796875

Your options:

  • round the numbers
  • use a i < 2.0 condition (but you might miss one iteration depending on rounding)
  • use a BigDecimal for an exact representation of 0.2

Upvotes: 2

Related Questions