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
badwolfffAsked:
Who is Participating?
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:
private static void loop4() {
		int i = 5;
	        while(5 < 10) {
		//while(i < 10) {
	        	System.out.println("In loop i = "+i);
	            	i++;
	        }

Open in new window

5 will always be less than 10 therefor infinite loop, so it will never reach the next line after the loop
comment out while(5<10) { and uncomment while(I<10) and you should get
5
6
7
8
9
10
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 (unreachable statement in line 13)?
Because the compiler knows why the statement you wrote is unreachable so will refuse to compile it
0
badwolfffAuthor Commented:
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
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

CEHJCommented:
And again
0
badwolfffAuthor Commented:
I am sorry I am a bit lost, do you mean I should give you assisted solution twice? Does that give you more points?
0
badwolfffAuthor Commented:
Ah sorry I didn't realise two different people had commented. My oversight. If I can fix it, I will happily do so.
0
CEHJCommented:
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
0
badwolfffAuthor Commented:
I will write to the moderators to change this and the other post too. Sorry about this.
0
CEHJCommented:
Don't worry this time but please note WHO is commenting in future
0
badwolfffAuthor Commented:
I will do. Sorry
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.