Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Unable to produce a simple while loop outcome.
Unable to produce a simple while loop outcome. [message #1454796] Tue, 28 October 2014 15:40 Go to next message
Eclipse UserFriend
My eclipse has ran every program ive made so far except one simple while loop. It doesn't give me any error messages and it looks as if its about to run the program but it just doesn't. Almost like its lagging but to test this I clicked run and went to sleep and woke up with still no result. Has anyone else had this issue? Here is a sample of my simple while loop just in case im doing something wrong with it:

public class Application {
	public static void main(String[] args) {
		
		int value = 0;
		
		while(value < 10);
		{
			System.out.println("Hello" + value);
			
			value = value + 1;
		}
	}
}


[Updated on: Tue, 28 October 2014 16:44] by Moderator

Re: Unable to produce a simple while loop outcome. [message #1454862 is a reply to message #1454796] Tue, 28 October 2014 17:14 Go to previous messageGo to next message
Eclipse UserFriend

I think if you replace

Quote:
while(value<10);


with

Quote:
while(value<10) // no semicolon


you will be ok.

Jim A.
Re: Unable to produce a simple while loop outcome. [message #1454889 is a reply to message #1454796] Tue, 28 October 2014 17:53 Go to previous messageGo to next message
Eclipse UserFriend
wow thanks man that worked perfectly! What is the reasoning behind that ? Im very new to java and am still learning the basics so the more you can explain I will be very appreciative.
Re: Unable to produce a simple while loop outcome. [message #1455045 is a reply to message #1454889] Tue, 28 October 2014 21:52 Go to previous message
Eclipse UserFriend

Hi Michael,

You just have to get used to the java syntax more (and it's short cuts).

I'm not a language guru, so my explanation here is not really too formal.

Anyway, the syntax for a while loop, is something like:

while ( condition ) code_block

where 'code_block' is executed if 'condition' is true.

Normally, a block of code is delimited by { }. But block can be simply a single statement,
so a while loop could be:

while ( condition ) System.out.println("Hi");

Note, the lack of brackets.

Now, if the single statement of the while loop is a null statement, it could look like

while ( condition ) ;

which will do nothing as long as condition is true. If the condition is true, it will always be true once the loop is entered.

The above while loop can also be written as:

while ( condition )
{
;
}

I hope this helps you.

Jim
Previous Topic:[Resolved] Define A New Server window does not allow server name entry
Next Topic:[RESOLVED]Unable to stop at breakpoint during remote debug session
Goto Forum:
  


Current Time: Mon Apr 28 19:39:32 EDT 2025

Powered by FUDForum. Page generated in 0.04435 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top