What are the differences between if and while?

The code snippet below works correctly for this exercise, but if I try to use ‘while’ instead of ‘if,’ the function returns a sum of 16000, instead of the correct answer of 9020. Why does ‘while’ not work instead of ‘if’ here?

32%20AM

if and while are different things, they shouldn’t do the same thing, should they?
or do you think if is wrong and that it should be while instead?
if you do it manually in your head/with pen/paper, then, which is it?

Hi ionatan - thanks for taking my question!

I don’t think ‘if’ is wrong here. It checks a condition about running_sum, and if true, it does the action called for. And again. And again. Until the condition is not true. This makes sense to me.

But, I still don’t see why ‘while’ doesn’t work just as well. Here, I want an additional element to be added to “running_sum” until some condition is reached - i.e., until running_sum > 9000. If running_sum is still <= 9000, I want an additional element to be added. And when running_sum > 9000, the ‘if’ condition is no longer met and there’s no additional in the ‘for’ loop, so at that point it ought to just return running_sum. This sounds to me like a situation for a ‘while’ loop. Right?

I’m not sure what I am missing.

The task isn’t to do the same thing repeatedly until a condition is met. And, if you think if does the right thing then you shouldn’t think while does the right thing as well since it does something else. Something other than the right thing, that’s the wrong thing.
But like I said, run it in your head, you should catch yourself doing the wrong thing, right? At least, if you’ve understood the task. And, similarly, you should know what the different things in your code do, so you should be able to run it correctly too.

1 Like

Thanks again for your input! I think I see why ‘while’ is incorrect here. My understanding is below - am I thinking about this correctly?

‘while’ leads to to the repeated addition of the element at the given index until the sum is greater than 9000. So it adds 8000 twice. So the first ‘while’ loop exceeds 9000, and we don’t end up running the ‘while’ loop on any subsequent elements of lst.

Whereas, ‘if’ leads to the addition of the element at the given index just one time. After an element has been added according to the code below the ‘if’ statement, we go back to the ‘for’ loop for instructions. And the ‘for’ loop instructs that we go to the next element in lst, and we get sent back down to evaluate if the condition for if statement is met.

Originally when I read the instructions to “sum the elements of the list until the sum” exceeds 9000, I jumped towards ‘while’ because of until. But ‘while’ is only helpful if you’re asked to do a repeated action - which I now understand is not the case here.

I’m reluctant to answer any of that because you can look for yourself. Both by reading, but with more certainty you can also print things out from the function while it’s running.

I can think of ways to use while, but I could of course not just throw it in there I’d still have to leverage it in ways that connects the initial state to the desired state. Like dominos.

1 Like

Got it. Thanks again!