aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes oddness while question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "oddness while question." Watch "oddness while question." New topic
Author

oddness while question.

WiLL Tao
Ranch Hand

Joined: Dec 14, 2001
Posts: 56

how many times are "Welcome to My World!" be printed?I compile and cann't understand the answer.Anyone can help?

[ April 17, 2002: Message edited by: WiLL Tao ]

Thank Folks who <b>Make Sense</b> here.<br />SCJP Platform 2
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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?


Dave
WiLL Tao
Ranch Hand

Joined: Dec 14, 2001
Posts: 56
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 ]
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
because the varible type is byte,so the max is 127=011111111
if you add 1 to 127,then the b is 10000000,it's negetive
Ase Lindskog
Greenhorn

Joined: Apr 08, 2002
Posts: 7
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.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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.


Rob
SCJP 1.4
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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 ]
Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
Hi Rob,
I am using pIII - 500Mhz
Time to loop 2147483647 times: 9070ms
Press any key to continue . . .

looks like my slower processor is faster than yours.


-Arun
geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
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?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Bah! I hate simple addition! :roll:
Ana P
Greenhorn

Joined: May 02, 2002
Posts: 10
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 ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: oddness while question.