• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Unreachability - diff results for diff version

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the java version j2sdk1.4.1_06

The statement:
for ( ; false ; );

Gives unreachable error.

but previous versions gives no error...
For exam which is the correct answer? Or how should i approach?
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which version of exam are you going to take ?
 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am taking SCJP 1.4 310-035
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Geethakrishna,

The statement:
for ( ; false ; );


According to JLS2 14.20 Unreachable Statements, that statement is considered unreachable.

for statement can complete normally iff at least one of the following is true:

  • The for statement is reachable, there is a condition expression, and the condition expression is not a constant expression with value true.
  • There is a reachable break statement that exits the for statement.


  • The contained statement is reachable iff the for statement is reachable and the condition expression is not a constant expression whose value is false.


    Since you're preparing for SCJP 1.4, I think it's safe as long as we follow the rules specified in JLS 2. So if being asked, I'd choose the one that gives unreachable error as the correct behavior from the compiler. Check out this bug report as well.

    If you're preparing for SCJP 5.0, you'll have to refer to JLS 3rd edition.

    Joyce
    [ June 24, 2005: Message edited by: Joyce Lee ]
     
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When it is said

    for ( ; false ; );

    Is it like this you are using false in the place of the i<10 ?

    for (int i =0 ;i<10; i++)
     
    Joyce Lee
    Ranch Hand
    Posts: 1392
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi latha,

    That's right. The general form of for statement can be expressed as:

    for (initialization; conditional expression; iteration expression)

    Both false and i<10 are in the conditional expression section. All of these three sections of for statement are optional; that is, the following code is legal:


    Joyce
    [ June 25, 2005: Message edited by: Joyce Lee ]
    reply
      Bookmark Topic Watch Topic
    • New Topic