Total C newbie -- PLEASE HELP with writing a LOOP, asap!!!!
Hi guys, please help me out as i am a complete newbie when it comes to C programming. I have the following code (as shown below). As you can see, it is a series of if/elseif statements. I'm very much a newbie in understanding loops (I do understand them but not how I might apply them to this situation, as with loops I generally have a condition, update, etc., and other than 'times' being the update (as in number of times), I don't see how I can apply this to a loop.
Basically, at the end of each of the FIRST TWO if statements, it ends in 'times=times+1' ... I just need to find a way to embed this whole thing in a LOOP so that at the end of each of these two statements, it goes back up to the "x = ..." line and begins from there again (with new values for 'guess', as assigned). Obviously, if it hits the 3rd if statement, no looping back will be necessary, so I imagine the 3rd if statement would somehow appear outside of the loop? Not sure.
Code:
x = guess*guess - n
if (|x|> tol && x>0)
{
tooHigh = guess;
guess=(tooHigh + tooLow)/2;
times= times + 1;
}
else if (|x|>tol && x<=0)
{
tooLow = guess;
guess = (tooHigh + tooLow)/2
times= times + 1;
}
else if (|x|<=tol)
{
print times;
return guess;
}