• March 25th, 2014, 05:16 PM
chakana101
in the case of:

Code :

```  int low = 10 , high =0   do {   System.out.println( low); low++; } while (low <= high );```

answer says that the loop is infinite, and will continue to produce values until int low cannot hold a big enough value, but my question is, why would the loop continue to be evaluated if low is 10 and high is 0, 10 is not <= 0, so wouldn't the loop stop after the first execution, and not continue to be evaluated?
• March 25th, 2014, 05:41 PM
GregBrannon
What answer says that? Provide a link to a reference if you can.
• March 25th, 2014, 05:57 PM
chakana101
this is the answer in the back of the book:

6.10:

the code contains an infinite loop. The numbers 10, 11, 12 and so on will be printed until the program is terminated or until the number gets to large to be held by the int variable low.
• March 25th, 2014, 06:03 PM
GregBrannon
Books can contain mistakes.
• March 25th, 2014, 07:50 PM
dicdic
Quote:

Originally Posted by chakana101
this is the answer in the back of the book:

6.10:

the code contains an infinite loop. The numbers 10, 11, 12 and so on will be printed until the program is terminated or until the number gets to large to be held by the int variable low.

As what you have said, loop should start after the first execution. so I guess you understand what the above program really do.
and if you have doubts on your observation just copy it and paste it in your main method, compile and execute.
actually it will not compile since the declaration of int does not end with semicolon, but fixing that I'm sure you will noticed that it only execute just once not infinite and prints 10. Maybe the book you are reading is not a good book. you might want to throw it in trash can LOL :D

Quote:

The numbers 10, 11, 12 and so on will be printed until the program is terminated or until the number gets to large to be held by the int variable low.
that statement is another proof that the book you are reading is not a good book to be read in java.
When the number gets to large to be held by the int data type, it will not make your program terminated or crashed. Actually it can still hold by the int variable, but it will have negative values, (I'm not sure if it is the two's compliment)
try to do this:
Code java:

```// putting the maximum value of int into max variable int max = Integer.MAX_VALUE; // then print it System.out.println(max); // it prints the maximum integer an int can hold //increment the max variable max++; /* as what the book says, it would terminate your program right? but actually, it would not. */ //print it System.out.println(max); /* this print statement will print the max variable, but you will notice that it has negative value```

I'm not if it is the the two's compliment of the value, but you can do some experimentation if you are curious about it.
• March 25th, 2014, 08:38 PM
chakana101