difference between two conditions in a while loop

Viewed 216

I have a question regarding my code. I know what I have to change in my code, but I will would like to understand why this is happening since I really want to learn the fundamentals. So my code is:

public class chunnimunni {
    public static void main(String[] args) {
         double number = 2.0;
         while (true) {
             if (number == 0.5)
                 break;
             number -= 0.1;
             System.out.printf("%8.1f", number);
         }
         System.out.println ("Finished.");
    }
}

This code will go on and print out the numbers indefinitely, it does not stop at 0.5. However if I change the while condition from:

while (true)

to

while (number > 0.5)

then it works.

If someone can explain the reason for this I would be very happy. Thanks.

1 Answers
Related