cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

Fast question

ptc-5898900
1-Newbie

Fast question

Hy.

I have a quick question. A simple algorithm.

I have a condition: a<L*B(that equals 20).

Initial values x=1,y=1, a=x*y.

Iterative function "while".

The result must stop at 16 (when x=4 and y=4; a=x*y=16<20). Well it doesn't. It stops one pace later (x=5, y=5, a=25).

How do I make stop at 16 not at 25?

P.S. I noticed that those initial values x=1,y=1,a=x*y are irrelevant. I removed them and the algorithm wasn't affected.

Thanks.

1 REPLY 1

You should provide an initial value for a - don't trust it will be 0 if you don't assign it a value!

Your routine returns the values of the variables after the loop stops and it will stop when a>20, so that is what you get. To return the values of the iteration step before the loop fails you have to store them in variables before you go into the next iteration and return these values at the end. Of course in your simple example you could simply return x-1 and y-1 without keeping variables for old values, but for real life iterations it won't be that easy to go back on the last iteration step that easy.

Top Tags