• 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

While loop Mystery

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

Can someone explain me why Case 1 gives Error
& why Case 2 Works well

Case 1:-

int n = 5;
while (n > 7)
int k = 2;

Case 2:-

int n = 5;
while (n > 7)
{
int k = 2;
}
Thanks.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
removed since it was a stupid answer.
sorry.
[This message has been edited by shadow liu (edited May 03, 2001).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shah,
I think it has to do with scope. In the first case you are trying to declare (maybe multiple times) the same integer k. Java stops you from declaring a variable more than one inside any block of code.
In your second case, you have created a block that limits the scope to that block (instead of the entire method/class). This allows you to declare the integer everytime through the loop.
Regards,
Manfred.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chunky,
Let me explain the reason of the problem.
In the first case,
the expression within the <code> while </code> is evaluated to false so the statement <code>int k = 2; </code> is not executed but as per JLS 14.11 . The <code>while </code> must have a block or and executable statement.
You can verify it by refering to the code below :

Having said that, in the second case the <code> while </code> loop does has ending block in the form of <code>{ } </code> . So the compiler doesnt complains. Please note that the statement inside the while loop is not executive in both
cases.
Manfred the code of chunky has nothing to with multiple declaration of the variable 'k'. That line just aint getting executed as the condition in while evaluates to falsity.
Hope this clarifies the issues.
Rgds,
Ravindra Mohan.

[This message has been edited by Ravindra Mohan (edited May 04, 2001).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic