What is the code doing exactly and what is the theoretical reason why this code is not compiling? (part 1)

Please see below some java code which I am trying to compile and run. I am fully aware that the code has an error. I also know how to fix it (as you can see from the comment just below). What I don't however know is what this code is actually doing.
Is it looping infinitely? Does it stop abruptly? What is the exact reason behind why I get a compile error?

import java.util.Random;

public class PartA {

    //loop 10 times
    private static void loop2() {
        for(;;) System.out.println("In loop");
        //for(int i = 0; i < 10; i++) System.out.println("In loop");
        System.out.println("Out of loop");
    }

        private static void runLoops() {
		loop2();
		System.out.println();
	}


	public static void main(String[] args) {
		runLoops();
	}

}

Open in new window


thanks in advance
badwolfffAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
  for(;;) System.out.println("In loop");

Open in new window

this is an infinite loop
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
What is the exact reason behind why I get a compile error?
Because the line the compiler will indicate is not reachable because it is preceded by an infinite loop
0
badwolfffAuthor Commented:
When there are infinite loops the java compiler tells me "code taking more than 5 seconds to execute" and it kills it (compilejava.net).
Here I get a different error: Unable to execute program; could not compile!
/tmp/java_kmr4s7/PartA.java:9: error: unreachable statement
        System.out.println("Out of loop");
        ^
1 error

So is this an infinite loop or something else? And if it is an infinite loop, why? What is the stop condition that is not being met?
thanks
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

CEHJCommented:
You're really just repeating the question, but:

When there are infinite loops the java compiler tells me "code taking more than 5 seconds to execute" and it kills it (compilejava.net).

I'm not sure why you're using compilejava.net but it isn't helping you. Why? Because you're not able to distinguish between compilation and execution, which you would be able to do if you were using standard tools (javac+java). If something is able to be killed, it must be running, which means it must have been able to be, and was, compiled.

The differences you're seeing are due to cases where the compiler knows a loop won't ever finish, such that the next statement is unreachable and cases where it doesn't know, but the loop turns out to be infinite anyway
0
badwolfffAuthor Commented:
Thanks. Got it.
0
CEHJCommented:
Why did you accept only ONE answer, which evidently didn't answer your subsidiary questions?
0
badwolfffAuthor Commented:
Ah I didn't realize that is how it is done in experts exchange. Sorry. Will keep it in mind next time unless I can change this selection.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.