Alic
Alic

Reputation: 648

Newton’s method for finding square roots in python

This is a function that uses Newton's method to solve for the square root of a given number:

def sqrt(n):
    approx = n / 2.0
    while True:
        better = (approx + n / approx) / 2.0
        if abs(approx - better) < 0.00001:
            return better
        approx = better

What I do not understand is, that the function only returns "better", which is a integer, but the while loop depends on a Boolean, so how is the while loop ever going to stop? Where does the function returns a False to terminate the while loop?

Many thanks in advance.

Upvotes: 0

Views: 4225

Answers (4)

Toalp
Toalp

Reputation: 362

when python gets to "return ..." in a function, no matter how many loops you are in, the function will stop, return the value, and continue with the rest of the code.

Where does the function returns a False to terminate the while loop?

if you really want to terminate the while loop without "exiting" the function, you can use the statement break.

def bla():
    a = 1;
    while True:
        ...
        ...
        if blichlach = 18273182736127836:
            break
        ...
    a = 2

this simple function, assign 1 to a, then get into infinite while loop, because True will always be true. then, when blichlach hits 18273182736127836, the loop will stop - and then the function will continue after the 'while', which mean it will assign 2 to 'a'.

def bla():
    a = 1;
    while True:
        ...
        ...
        if blichlach = 18273182736127836:
            return 1;
        ...
    a = 2

in this one, when blichlach hits 18273182736127836 in the while loop, the function will return to the main code, or the function that called it. 'a' will never be 2.

Upvotes: 0

user2555451
user2555451

Reputation:

Actually, it never "returns a False to terminate the while loop" because it simply doesn't need to.

When Python hits a return-statement inside a function, it immediately exits that function. Meaning, when return better is reached, sqrt is exited and, in the process, so is the while-loop.

Upvotes: 4

murgatroid99
murgatroid99

Reputation: 20252

The boolean that you mention, True, is the condition on which the loop repeats. In this code, the loop always repeats because the loop condition is always True. The loop condition does not depend on the return value. When the return statement is reached, the function stops and returns that value immediately, even if the return statement is in loops or if statements.

Upvotes: 2

Abhijit
Abhijit

Reputation: 63707

so how is the while loop ever going to stop?

It will stop when the function returns. A function return transfers the control to the caller thereby involuntarily breaking and terminating from the loop.

Where does the function returns a False to terminate the while loop?

It doesn;t need to. Assuming, the function calculates square root of non negative real numbers, the function is guaranteed to return. Thus resulting loop termination

but the while loop depends on a Boolean

The example loop is a classic example of an Infinite Loop. Infinite Loop guarantees to run forever, until an external scenario forces an exit from the Loop. Explicit conditions can either be

  1. An Explicit Break Statement
  2. A Function Return
  3. Any implicit scenario like interrupt, signals, or Exceptions.

Upvotes: 0

Related Questions