Link to home
Start Free TrialLog in
Avatar of badwolfff
badwolfffFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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 (unreachable statement in line 13)?

import java.util.Random;

public class PartA {

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

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


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

}

Open in new window


thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What is the exact reason behind why I get a compile error (unreachable statement in line 13)?
Because the compiler knows why the statement you wrote is unreachable so will refuse to compile it
Avatar of badwolfff

ASKER

Hi thanks for your comments. Both are useful. I have one follow on question:

I am thinking what the code does step by step.

First it selects a variable and sets that integer's value to 5
Then it sets a stop condition (faulty in this case) requiring 5 to be less than 10, which is always TRUE
That means the condition inside the while function is met and code inside the while braces {} is reachable
But I don't see any output for System.out.println("In loop i = "+i);
If the code inside the braces is reachable then the value of the variable i is incremented at every loop cycle (but this is inconsequential as it is never really used)
If my logic above is right then the code inside the braces is being executed, so why does the code outside (System.out.println("Out of loop");) not get executed at least once or over and over again?
What is the actual reason why it is becoming unreachable? And therefore, why do I get a "compile error" instead of a "code taking too long to execute" error due to infinite loop?

thanks
And again
I am sorry I am a bit lost, do you mean I should give you assisted solution twice? Does that give you more points?
Ah sorry I didn't realise two different people had commented. My oversight. If I can fix it, I will happily do so.
do you mean I should give you assisted solution twice?
(My emphasis)
That would have been correct (since i addressed your question precisely, in addition to the general terms supplied by others) but actually even once would have been better
I will write to the moderators to change this and the other post too. Sorry about this.
Don't worry this time but please note WHO is commenting in future
I will do. Sorry