• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop not compiling

 
mvPrasad Regula
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been changed
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Atwal Usha
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
Harsh Pensi
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, compiler gives Unreachable code error with while(false){}, but not with if(false){}.
 
KrishnaPrasad raghavan
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just Remove the ; after the for( ; ; ) ;. I am sure the compiler won't complain.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Visit this link to get some idea about compile time resolution

Compile time constant
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic