using break to redirect to a loop

So, I've got a bit of code that gets a user input, and if the number is less than zero it asks the user to try again and should get another input, but it doesn't work. It doesn't seem to evaluate the second input. Here's my code:

System.out.print("Enter the number to be evaluated: ");

double N = sc.nextDouble();

loop1 :if (N < 0)

{

System.out.println("ERROR: Cannot evaluate a negative number.");

System.out.print("Try again: ");

N = sc.nextDouble();

if (N < 0)break loop1;

[776 byte] By [Kronoa] at [2007-11-27 9:41:08]
# 1
Eww... labels make my skin crawl.Suggestion: write a *subroutine* to get a non-negative number.
BigDaddyLoveHandlesa at 2007-7-12 23:19:29 > top of Java-index,Java Essentials,New To Java...
# 2

> Eww... labels make my skin crawl.

>

> Suggestion: write a *subroutine* to get a

> non-negative number.

Well, labels are actually not that bad in java, but I seldom use them for the same reason. ;-) But I think the OP missunderstood them: You cannot jump to loop just exit a loop: break loop1 will exit the loop with the label loop1.

-> using labels you don't have to set flags and test for them. Can be usefull in nested loops to exit the outer.

-Puce

Pucea at 2007-7-12 23:19:29 > top of Java-index,Java Essentials,New To Java...
# 3

So, would this be better?

loop1 : while (N < 0)

{

System.out.println("ERROR: Cannot evaluate a negative number.");

System.out.print("Try again: ");

N = sc.nextDouble();

if (N > 0) break loop1;

}

I tested it and it seems to work, or would this be considered bad code?

Kronoa at 2007-7-12 23:19:29 > top of Java-index,Java Essentials,New To Java...
# 4
The label is unnecessary there. And so, for that matter, is the break.
jverda at 2007-7-12 23:19:29 > top of Java-index,Java Essentials,New To Java...
# 5
But what if they enter 0? You have two different behaviors suggested in your code.
jverda at 2007-7-12 23:19:29 > top of Java-index,Java Essentials,New To Java...
# 6
> But what if they enter 0? You have two different> behaviors suggested in your code.What are the odds that they choose 0 for a double value? infinitesimal!;-)
BigDaddyLoveHandlesa at 2007-7-12 23:19:29 > top of Java-index,Java Essentials,New To Java...
# 7
0 isn't an option since this is just a part of a newton square root algorithm.
Kronoa at 2007-7-12 23:19:29 > top of Java-index,Java Essentials,New To Java...
# 8
isn't the square root of 0, er, 0?
BigDaddyLoveHandlesa at 2007-7-12 23:19:29 > top of Java-index,Java Essentials,New To Java...
# 9
> > But what if they enter 0? You have two different> > behaviors suggested in your code.> > What are the odds that they choose 0 for a double> value? infinitesimal!No, just a little greater than 1/2^64, I think. ;-)
Pucea at 2007-7-12 23:19:29 > top of Java-index,Java Essentials,New To Java...
# 10
> 0 isn't an option since this is just a part of a> newton square root algorithm.Well, the last code you posted has two different ways of exiting the loop. One allows zero and the other doesn't.
jverda at 2007-7-12 23:19:29 > top of Java-index,Java Essentials,New To Java...
# 11

Actually, they both allow 0. the answer , not surprisingly, is 0.

I've got another question too. If anyone is familiar with the Newton square root algorithm, please help me out. I am aware that I need something like this:

double oldGuess = N;

double newGuess = N / 10;

while ((oldGuess - newGuess) > .000001)

{

newGuess = ((N / oldGuess) + oldGuess) / 2.0;

oldGuess = newGuess;

but this isn't correct, because the square root of 25 definitely isn't 13. I'm pretty sure it is a problem with assigning the value of newGuess to oldGuess. This makes the loop exit, and I don't want it to do that, I want it to check the condition ((oldGuess - newGuess) > .000001)

before assigning oldGuess the value of newGuess. How would I go about doing that?

Message was edited by:

Krono

Kronoa at 2007-7-12 23:19:30 > top of Java-index,Java Essentials,New To Java...
# 12
> Actually, they both allow 0. No.Look at your while loop.Then look at your (pointless) if condition.Your while prevents entering the loop if N == 0. Your if condition does not break if N == 0.
jverda at 2007-7-12 23:19:30 > top of Java-index,Java Essentials,New To Java...
# 13

>>Then look at your (pointless) if condition.

>>Your while prevents entering the loop if N == 0. Your if condition does not >>break if N == 0.

I took out the if and it's just while now. The way it is now, you could enter 0.

while (N < 0)

{

System.out.println("ERROR: Cannot evaluate a negative number.");

System.out.print("Try again: ");

N = sc.nextDouble();

}

Kronoa at 2007-7-12 23:19:30 > top of Java-index,Java Essentials,New To Java...
# 14

public double getDouble(Scanner sc) {

System.out.print("Enter the number to be evaluated: ");

double x;

while ((x = sc.nextDouble()) < 0) {

System.out.println("ERROR: Cannot evaluate a negative number.");

System.out.print("Try again: ");

}

return x;

}

There should be error handling code for misformatted input, too.

BigDaddyLoveHandlesa at 2007-7-12 23:19:30 > top of Java-index,Java Essentials,New To Java...
# 15

> Your while prevents entering the loop if N == 0. Your

> if condition does not break if N == 0.

Yes, but then the while-condition does not hold anymore and break, so 0 was allowed also with the first version, before and after the while-condition. Still the if-condition is not needed. :-)

-Puce

Pucea at 2007-7-21 23:06:43 > top of Java-index,Java Essentials,New To Java...
# 16

> >>Then look at your (pointless) if condition.

> >>Your while prevents entering the loop if N == 0.

> Your if condition does not >>break if N == 0.

>

> I took out the if and it's just while now. The way it

> is now, you could enter 0.

So change your while condition if you don't want to allow 0.

jverda at 2007-7-21 23:06:43 > top of Java-index,Java Essentials,New To Java...
# 17

> > Your while prevents entering the loop if N == 0.

> Your

> > if condition does not break if N == 0.

> Yes, but then the while-condition does not hold

> anymore and break, so 0 was allowed also with the

> first version, before and after the while-condition.

I know. I just wanted to point out the inconsistency between his two stopping conditions.

jverda at 2007-7-21 23:06:43 > top of Java-index,Java Essentials,New To Java...