# Project 2 tips Project 2: Compute Roots Using Newton Iteration

This lab introduces something we'll use pretty often in projects. We ask you to implement a loop such that the user is asked to enter a letter, and if its 'y' you repeat, otherwise you quit. A few things: First, you don't have to do crazy format checking. Assume its a single character given as input. Second is the type of loop we use. Avoid using a for loop in this scenario, as those are generally use only for iterating. We can use a do-while or a while loop. Third, now that we're using a while or a do-while loop, we should aim to minimize code. Too often I see students write their code like this:

``` output.println("Enter a number to take the square root of: "); double inputDouble = input.nextDouble(); result = sqrt(inputDouble); output.println("The square root of " + inputDouble + " is " + result); output.println("Would you like to go again?(y to continue): "); char response = input.nextChar(); while(response == 'y'){ // ... literally do the exact same thing you did up there ... } input.close(); output.close(); ```

You're reusing lines of code here when you could simplify the structure! Here's a better way to code it:

``` char response = 'y'; while(response == 'y'){ output.println("Enter a number to take the square root of: "); double inputDouble = input.nextDouble(); result = sqrt(inputDouble); output.println("The square root of " + inputDouble + " is " + result); output.println("Would you like to go again?(y to continue): "); char response = input.nextChar(); } input.close(); output.close(); ```

This way, we have a more readable chunk of code, and it uses less lines. Use this kind of structure whenever you run into the whole "y to continue" thing projects ask you to do. Also, here is an example of a do-while loop, my prefered way to code this. The do-while loops HAVE to happen at least once, so it tells a reader of your code "okay, this has to execute at least once", whereas the while loop doesn't HAVE to, but in the case above does only because we set response equal to 'y'.

``` char response = 'y' do{ output.println("Enter a number to take the square root of: "); double inputDouble = input.nextDouble(); result = sqrt(inputDouble); output.println("The square root of " + inputDouble + " is " + result); output.println("Would you like to go again?(y to continue): "); char response = input.nextChar(); } while(response == 'y'); input.close(); output.close(); ```

Keep in mind that percent is not the same a proportion. 1% of x means 0.01*x. 0.01% of x means 0.0001*x. This means that ε is actually 0.0001, since we want 0.01%.

In `sqrt()`, the error is relative to the input number, x - not the current "guess" that you're using.