• 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:

for loop not compiling

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Temp
{
public static void main(String[] args)
{
int i = 0;
for ( ; i<10; i++) ; // (1)
System.out.println("1= "+i);
for (i=0; ; i++) break; // (2)
System.out.println("2= "+i);
for (i=0; i<10; ) i++; // (3)
System.out.println("3= "+i);
for ( ; ; ) ; // (4)
System.out.println("4= ");
}
}

When I compile javac Temp.java, I get an error For the fourth System.out.println as "statement unreachable". Has it been at runtime I can consider that the fourth for loop is an infinite so the System.out.println is unreachable. This is compile time error.
 
mvPrasad Regula
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has been changed
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Has it been at runtime I can consider that the fourth for loop is an infinite so the System.out.println is unreachable. This is compile time error.



The compiler has logic to detect, under certain conditions, if code is unreachable. In this case, it can detect that the fourth for-loop is an infinite loop.

Henry
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

As the for loop at line no. 4 is an endless loop so compiler will never be able to reach any statement after that for the compilation purpose and therefore an error is thrown stating it to be an unreachable statement.

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give some more examples of such non-trivial issues where the compiler IMO behaves over-smart??? Got my exam on Monday and still new things coming up
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that in some cases the compiler is not smart enough to detect something leaving aside being oversmart.


Here the compiler detects that i is initialized but doesn't give an error that the else part is unreachable...
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, compiler gives Unreachable code error with while(false){}, but not with if(false){}.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just Remove the ; after the for( ; ; ) ;. I am sure the compiler won't complain.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Visit this link to get some idea about compile time resolution

Compile time constant
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic