How exactly do you track the exact amount of iterations? I know its probably basic but can't figure out how so was hoping someone could explain to a complete beginner how you arrive at an answer.

def f(n):

i = 4

 while i < n:


 i = i + 1

( The answer should be max(n−4,0) )

  • $\begingroup$ It's not basic at all. There's no general rule to count the exact number of iterations. However, for many common programs you can mentally count them. Above we start counting from $4$ until $n$ is met, hence $n-4$. If that would be negative, instead ... $\endgroup$
    – chi
    Mar 21 '17 at 15:00
  • $\begingroup$ Try adding some print statements inside all this and just observe the results. $\endgroup$
    – SDsolar
    Mar 23 '17 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.