[java] NEWBIE, best way to loop?

Started by
6 comments, last by CodyVa 23 years, 7 months ago
I posted this before... what''s the best way to wait till the user puts valid input in? java has no goto statement right?? so what is the best way to go back to the beginning of the method and get input again?
Advertisement
You could set up the method that gets the input as a thread so that is was called every so many fractions of a second.
You could set up the method that gets the input as a thread so that is was called every so many fractions of a second.
This is a REALLY newbie question.

You are talking about goto. goto is a no no in any modern language and even if it was present you shouldn't use it.

NEWBIE questions diverse simple answers,
so to check input the way you describe write:

do
{
< call function that retrieves input >
}
while ( <input is invalid> )

The loop will exit when the input is valid.
Put your own code within the < >.

Jacob Marner


Edited by - felonius on September 3, 2000 11:50:04 AM
Jacob Marner, M.Sc.Console Programmer, Deadline Games
...sorry, your soo right?

I also have confusion too
&& is and right?

so i can do { getnumber}
while {inputISvalid() != true && counter != 10}



am i right in inputISvalid() != true
inputISvalid returns true if it''s valid input
so I shouldn''t go through the loop again if it''s true right?

the && counter != 10 is also evaluated

say it''s not valid input and the counter is 10?
does it loop again? I don''t think it should.

Thanks alot felonius!
&& means logical AND and you use it correctly.

A && B.

Don''t mix it up with & which is something else entirely.

If A is true then B is evaluated (otherwise not)
and if B also is true then the complete
expression is true.

Note: because inputISValid() returns a boolean
it is already a boolean so you could rewrite
inputISvalid() != true
as
!inputISvalid()

When the expression in the while() is false
the loop ends and are not evaluated any more.

So the loop will end when the input is valid or
when then counter reaches 10.

There is a variation:
while ( )
{

}

While the do{}while() structure always run the
statements in the {} at least once and then does the
test the strcuture above makes the test before it
enters the first time.

Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Do while and while loops are the best ways to wait for valid input in any language. For the loop to be executed both conditions must be true when using the double ampersand.

I personally prefer the while loop to the do while, but you can go either way. Even if there was a goto available in Java it wouldn''t be good to use it. In most cases using goto statements leads to awful code. Avoid them whenever possible.

Adam

P.S. Don''t worry about asking a newbie question. We all start out as newbies.

I''m mean. use goto

JoeMont001@aol.com www.polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911

This topic is closed to new replies.

Advertisement