Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 19:40 Go to next message
michael mccarty is currently offline michael mccartyFriend
Messages: 2
Registered: October 2014
Junior Member
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 20:44] by Moderator

Report message to a moderator

Re: Unable to produce a simple while loop outcome. [message #1454862 is a reply to message #1454796] Tue, 28 October 2014 21:14 Go to previous messageGo to next message
Jim Anderson is currently offline Jim AndersonFriend
Messages: 109
Registered: October 2014
Senior Member

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 21:53 Go to previous messageGo to next message
michael mccarty is currently offline michael mccartyFriend
Messages: 2
Registered: October 2014
Junior Member
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] Wed, 29 October 2014 01:52 Go to previous message
Jim Anderson is currently offline Jim AndersonFriend
Messages: 109
Registered: October 2014
Senior Member

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 Dec 02 16:14:12 GMT 2024

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

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

Back to the top