Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Unreachable code in for?

 
Greenhorn
Posts: 12
Netbeans IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


Can you help me here?


this one, compilation fails

this one, infinite loop.


If the condition is false, then the for is useless right? why the unreachable code?


Thanks in advice
Alejandro Casarrubias
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the unreachable code is the i++. In Eclipse, a warning is also produced that says: Dead code. Clicking on the warning highlights the i++.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The empty block is the unreachable statement.
The i++ is not a statement, the empty block is a statement.

Try this code and see which line will the compiler mark as error
 
Rancher
Posts: 377
Android Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This happens because the compiler is clever enough to know that this code can never be reached.

but what about this:



this now compiles as it does not know what the runtime value of b will be. Even though we know it will always be false.

Sean
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The empty block is the unreachable statement.


The following code produces the same error:
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:

The empty block is the unreachable statement.

The following code produces the same error:

In that case, the unreachable statement must be that infinitely small gap between the closing paren and the semicolon! ;)
 
Alejandro Casarrubias
Greenhorn
Posts: 12
Netbeans IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys =)
the same here


the

nothing here

it's code never reached too... a bit odd



I guess, FOR as SWITCH must have predefined variables on the stack to play(in switch constant values assigned on the declaration)





Java tutorial says:


but it's more like,



it prints
1

Thanks to all!
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In that case, the unreachable statement must be that infinitely small gap between the closing paren and the semicolon! ;)


In Eclipse, clicking on the error actually highlighted the semicolon. Replacing the ';' with '{ }', Eclipse highlighted the '{ }'.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Byron wrote:In that case, the unreachable statement must be that infinitely small gap between the closing paren and the semicolon! ;)


It is actually the semicolon.

In Java semicolon is part of preceding statement. Empty statement is just a semicolon.
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
David Byron
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vlado Zajac wrote:

David Byron wrote:In that case, the unreachable statement must be that infinitely small gap between the closing paren and the semicolon! ;)


It is actually the semicolon.

In Java semicolon is part of preceding statement. Empty statement is just a semicolon.



Er... that was a joke. ;)
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic