• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

while loop

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here in both code 1 and code 2 there is no difference the condition is true for both but for code 2 it displays unreachable code and for code 1 it displays one time



sombody help me out please
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The expression ++b>0 cannot be evaluated at compile time, so there is no error.
Make b variable final and change the expression to b > 0, then you'll see the compiler error.
[ August 03, 2006: Message edited by: Jasiek Motyka ]
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

In the first loop the b variable is not final.so the b can be modified but if you define b as final then it will give compilation error.

In seccond case always the condition will go as false so the while statement is becoming unreachable.


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


A while statement can complete normally iff at least one of the following is true:
.The while statement is reachable and the condition expression is not a constant expression with value true.
.There is a reachable break statement that exits the while statement.

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the first while loop is an endless loop, hence the second while loop(or for that matte, anything following the first while loop) is unreachable.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable b is a byte. So, its range is 127. the condition "while (++b >0)" will be true till 127 and in the next loop, vluue of b is negative ((byte)(127+1).and the condition will be false. So, the message will be displayed.
For the second loop, the condition is true for-ever.and hence.. un-reachable code.




sombody help me out please[/qb]<hr></blockquote>

[ August 03, 2006: Message edited by: Suchit Pati ]
[ August 03, 2006: Message edited by: Suchit Pati ]
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Reply Wise,

What wise has said is right. What you have is a do nothing while loop , which does not satisfy any of the rules that wise has listed out. Since the do nothing (second while) loop has a constant expression which is evaluating to true it violates first rule and does not contain a break which happens to be the second rule. Thats why you are getting unreacheable code.

try changing the second while to this,



It should work fine for the above reasons.

Bye & Regards,
Praveen

[ August 04, 2006: Message edited by: praveen babu ]
[ August 04, 2006: Message edited by: praveen babu ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic