Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

while loop

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

public void abc( ) {
byte b1 = 1;
while ( ++b1 > 0 ) ;
System.out.println("Welcome to Java");
}
this code will print "Welcome to Java"
so i want to know what is the condition for the while loop to be endless loop.
thanks if you help me
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A while loop continues as long as the logic test returns true - assuming there is no code inside the loop to break or return or something similar. In your example as long as b1 is positive. I suspect that when b1 is incremented to 128, it will be evaluated as negative and the loop will stop. If you want an endless loop try while( true )
Bill

------------------
author of:
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ronak,
the reason why your while loop only prints "Welcome to Java" once, is because you have a do-nothing while loop.
while ( ++b1 > 0 ) ;
The "while" executes the statement that immediately follows it. Statements, in Java, are signified by a line ending with a ";". Java ignores whitespace. So the ";" at the end of the line your while is on tells Java to run an empty statement as long as "++b1 > 0" resolves to true.
What you have is identical to:
while ( ++b1 > 0 )
{
;
}
System.out.println("Welcome to Java");
Bill's right about the number of times it does nothing... incrementing b1 will result in a positive right up to the point it overflows and becomes a negative. So your code does nothing 127 times, then prints out "Welcome to Java".
(actually, I'm not certain it really does nothing 127 times, maybe it's smart enough to recognize the futility and completely skips it)

[This message has been edited by Guy Reynolds (edited August 17, 2001).]
 
ronak mehta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to all who help me. ronak.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn't prefix increment converts the byte to int? Then the do nothing loop would be more than 127 times, right?
 
Guy Reynolds
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. It doesn't convert the byte to an int...
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I tried Bill Brogden's suggestion and altered Ronak's code as follows...


But the compiler doesn't like it
It said...

Any Comments?!
Cheers
Shyam

[This message has been edited by Shyamsundar Gururaj (edited August 21, 2001).]
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, remove semicolon ( after while loop....you will get an infinite loop. or remove println statement........


class DeleteLater {
public static void main(String[] args){
byte b1 = 0;
while (true)
System.out.println("Welcome to Java");
}
}

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose the program goes into an endless loop executing the
while( true ); statement.All statements after this are therefore unreachable.I tried out the following which too give a compiler time error of unreachable statement :
while(false)
System.out.println(" hello" );

I did not understand as to why the byte is not converted to int ?
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,

Originally posted by Angela Narain:
I did not understand as to why the byte is not converted to int ?


Are you referring to the increment in the first example?
If so, it is not promoted to an int because arithmetic promotion does not occur for prefix and postfix operators.
Jane has a good review of this in her notes on these pages:
http://www.janeg.ca/scjp/oper/prefix.html
<a href="http://www.janeg.ca/scjp/oper/promotions.html<BR rel="nofollow">">http://www.janeg.ca/scjp/oper/promotions.html
--liz
[This message has been edited by Elizabeth Lester (edited August 22, 2001).]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iam a new furam about this programe so i would like to know about this.
please reply.
thanks a lot.
emran.
 
reply
    Bookmark Topic Watch Topic
  • New Topic