Will Because of the ; after the while loop the loop will execute endlessly. The ; represents an empty statement and because it immediatly follows the while loop it is that statement that will be executed in the loop. So the answer to the question is that 'Welcome to My World!' will never be printed. If the loop ever exited (you cahnged the conditions of it) then it would get printed once. Did you try the code yourself?
Joined: Dec 14, 2001
Hey,Dave I'm so carefulless for ignoring ;
This is question from j@Wiz.I simply copy and paste;compile. Shame!!! Thank you for great explaining. [ April 17, 2002: Message edited by: WiLL Tao ]
Hei guys, this is not an endless loop. Byte b will overflow and become -128 ,then exit from the loop. So "Welcome to My World!" will be printed exactly once.
Joined: May 10, 2001
Actually Andy and Ase the variable would be promoted to an int (see here) and thus wouldn't overflow until after it hit 2,147,483,647 which would probably be enough to hang most computers. Instead of the ; put in this line: System.out.println(b); and see what happens Dave
Actually Dave, the post/pre increment operator performs a narrowing conversion for you, so the type of the result value is the same as the original operand, in this case byte.
15.15.1 Prefix Increment Operator ++ A unary expression preceded by a ++ operator is a prefix increment expression. The result of the unary expression must be a variable of a numeric type, or a compile-time error occurs. The type of the prefix increment expression is the type of the variable. The result of the prefix increment expression is not a variable, but a value. At run time, if evaluation of the operand expression completes abruptly, then the prefix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (�5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (�5.1.3) to the type of the variable before it is stored. The value of the prefix increment expression is the value of the variable after the new value is stored.
Joined: Jan 07, 2002
One more thing, on my Pentuim III 1.2Ghz machine, I can loop from 0 to Interger.MAX_VALUE in about 15 seconds.
Prints out: Time to loop 2147483647 times: 15222ms Now, by extrapolating this run time, the time it would take to loop over the range of a double variable would be 2073 years. That's a pretty long loop [ April 17, 2002: Message edited by: Rob Ross ]
Will, I tried ur code,it prints "welcome to my world" only once. As the while statement has a semicolon at the end,it doesn't function as a loop,right. So,the System.out.println("welcome to my world") functions as a normal statement,as it would outside a loop & prints it once. Want to know, if my interpretation is right?
Originally posted by geetha nagarajan: As the while statement has a semicolon at the end,it doesn't function as a loop,right.
No! The while loop is still a loop! The semi-colon is an empty statement, meaning that it doesn't perform any action (like a NOOP in assembly). What happens in this case is that the empty statement (the semicolon at the end of the loop) is executed 125 times (if my math is correct) and then the variable b (which is of type byte) will overflow and its value will become -128, which cuases the loop condition to become false. At this point, the loop exits and execution begins at the next line following the loop, which prints out "Welcome to My World!" I hope that helps, Corey
the empty statement (the semicolon at the end of the loop) is executed 125 times (if my math is correct) and then the variable b (which is of type byte) off by one, Corey: When b == 1, loop has been executed 0 times. When b == 127, loop has executed 126 times. b is incremented, value of b wraps around and becomes negative and the expression is false, terminating the loop after 126 iterations. Or you could just stick a counter variable in the loop and print out the results, which is what I actually did Junilu
how can in while loop b++ calculates -128 binary value of 127 is 0111 1111 + 1 ----------- 1000 0000 pls explain... byte b1 = 1; while ( ++b1 > 0 ); System.out.println(b1); //prints -128 ??? System.out.println("Welcome to My World!"); [ May 15, 2002: Message edited by: Ana P ]
Joined: Dec 20, 2001
Originally posted by Ana P: how can in while loop b++ calculates -128 binary value of 127 is 0111 1111 + 1 ----------- 1000 0000 pls explain...
I don't know what you're confused about. You've written the answer right there. The binary version of 127 is 01111111 and, if you add 1 to that, you get 10000000, which is the binary version of -128. Remember, the left-most bit is a sign bit, not a data bit. If you're still confused, please elaborate. Corey