• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

oddness while question.

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
WiLL Tao
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bah! I hate simple addition! :roll:
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic