# Help Understanding While loops

**URL:** <https://discuss.python.org/t/help-understanding-while-loops/37827>\
**Category:** Python Help\
**Created:** [November 3, 2023, 4:50pm UTC](https://discuss.python.org/t/help-understanding-while-loops/37827 "2023-11-03T16:50:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbrandon12](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@sbrandon12](https://discuss.python.org/u/sbrandon12)\
**Post date:** [November 3, 2023, 4:50pm UTC](https://discuss.python.org/t/help-understanding-while-loops/37827/1 "2023-11-03T16:50:53Z")

</div>

Hello, I have been learning python using Solo learn. It has been a good source for me. I have made it to this point once and I restarted the course again. I am stuck at loops again. This seems to be an important part. If anyone could help me understand what is happening in this line of code I would appreciate it.

`counter = 0 while counter < 4: print(counter) counter = counter + 1`

 ![Screenshot 2023-11-03 at 12.44.54 PM](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/1/12649a1e0601a6a8b6a404faba33b35c4f3aac59.png)

Just to give a little background. This is a screenshot from solo learn.  
I don’t understand the “+1” how that works.  
I understand that it helps close the loop but I don’t know how.  
The iteration. Is that the number of times the loop has cycled?  
I have looked online for some answers I can find more complex problems that confuse me more. I think if I can understand this it will help to understand more complex lines of code. If anyone would be willing to take the time to help I would greatly appreciate it.  
Thanks!

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [November 3, 2023, 4:59pm UTC](https://discuss.python.org/t/help-understanding-while-loops/37827/2 "2023-11-03T16:59:43Z")

</div>

The way that python changes the value of a variable is to use `=`.  
`=` does not mean equality as it does in mathematics.

To set `counter` to 0 you have this line of code:

```auto
counter = 0

```

Inside the loop the value of counter needs to be incremented by 1 each time.

```auto
counter = counter + 1

```

What that does is take the current value of counter, add one to it and replace the value in counter with the calculation.

So it counter is 3 then `counter + 1` will be 4.

Does that help?

---

<div class="post-metadata">

**Author:** ![sbrandon12](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@sbrandon12](https://discuss.python.org/u/sbrandon12)\
**Post date:** [November 3, 2023, 5:19pm UTC](https://discuss.python.org/t/help-understanding-while-loops/37827/3 "2023-11-03T17:19:04Z")

</div>

Thank you for your quick response.  
So, in this case or all cases, the “=” sign does not mean that that is that value?  
“counter” is holding that “0” value?

tell me if my mindset it right in how I understand this.

The code starts with 0 as long as its less than 0 it will print  
the counter “counter = counter +1” creates the end point?  
so it adds one to each “iteration” ?  
and an iteration is a complete loop of the “loop” itself  
it uses that to come up with the “4 = 3+1” and that is how it ends the loop?

I haven’t had that “ah ha” moment yet. I seems that I am missing an understanding of the equation. Thanks again for your help

---

<div class="post-metadata">

**Author:** ![effigies](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/effigies/32/35835_2.png) [@effigies](https://discuss.python.org/u/effigies)\
**Post date:** [November 3, 2023, 5:51pm UTC](https://discuss.python.org/t/help-understanding-while-loops/37827/4 "2023-11-03T17:51:07Z")

</div>

The thing to understand is that there’s no such thing as an equation in Python. There are expressions and statements. What you are looking at here:

```auto
counter = counter + 1

```

is an assignment statement. The way to read this in general is “assign the value on the right hand side to the name on the left hand side”. On the first time through the loop, the value on the right hand side is `0 + 1`, which evaluates to 1. Then the value 1 is stored with the name `counter`.

Do not get hung up on the equals sign. To avoid this exact confusion, some languages use `<-` for assignment to show that the value on the right is being put in the variable named on the left. Note that you cannot do the reverse:

```python
>>> counter + 1 = counter
  File "<stdin>", line 1
    counter + 1 = counter
    ^^^^^^^^^^^
SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?

```

If it were an equation, these would be equivalent. You can think of it as “make the left side equal to the right side”, but I would just get in your head that there’s no such thing as an equation.

* * *

So to break it down. you have an assignment statement:

```auto
counter = 0

```

Then you have a while loop:

```python
while counter < 4:
    ...

```

This will check the value of the expression `counter < 4`. If the expression evaluates to `True`, then `...` is run and the loop will start again with another check.

What does it run before the next check? First it will print out the value stored by the name `counter`.

```auto
    print(counter)

```

On the first time through the loop it will print `0` to the console. Every time you go through the loop, it will print out whatever value is stored by that name.

Then we assign a new value to the name `counter`:

```auto
    counter = counter + 1

```

The first time through the loop, we assign `counter = 1`. The second time through the loop (since `1 < 4`) the value `1` will be printed to the console, and then we will assign `counter = 2`. And so on.

> The iteration. Is that the number of times the loop has cycled?  
> …  
> so it adds one to each “iteration” ?  
> …  
> and an iteration is a complete loop of the “loop” itself

Your last question gets it right. An iteration is a time through the loop, and the number in your screenshot is just a count of the number of times you’ve been through the loop. This is just for your understanding; it doesn’t correspond to any Python concept (there’s no secret place where the iteration number is kept track of). Crucially the iteration number is entirely independent of any value. The correlation to the values of `counter` are entirely incidental.

> “counter = counter +1” creates the end point?

All this does is update the value of counter. The loop stops because once `counter` points to the value `4`, the check `counter < 4` evaluates to `False`, and the `while` loop terminates.

> it uses that to come up with the “4 = 3+1” and that is how it ends the loop?

Let’s replace the `=` with a `<-` for the sake of breaking the connection with equations. When `counter` has the value `3`, then you can think of the increment line as going through the following transformations:

```auto
counter <- counter + 1 # Substitute the name counter for its value (3)
counter <- 3 + 1 # Add the two values (3 and 1)
counter <- 4 # Assign the value (4) to the name counter

```

---

<div class="post-metadata">

**Author:** ![sbrandon12](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@sbrandon12](https://discuss.python.org/u/sbrandon12)\
**Post date:** [November 3, 2023, 5:55pm UTC](https://discuss.python.org/t/help-understanding-while-loops/37827/5 "2023-11-03T17:55:33Z")

</div>

I have only read the first two small paragraphs and already had an “ah ha” moment. I am going to keep reading. Thank you so much for this good information. This is was I was looking for. Thank you thank you given me a little hope in understanding this more and more.

---

<div class="post-metadata">

**Author:** ![sbrandon12](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@sbrandon12](https://discuss.python.org/u/sbrandon12)\
**Post date:** [November 3, 2023, 7:19pm UTC](https://discuss.python.org/t/help-understanding-while-loops/37827/6 "2023-11-03T19:19:27Z")

</div>

Work slowed down and I was able to read the rest of your post. Thank you again. It makes so much more sense. I can picture it like the flow chart it would be and where the loop is after your explanation. Well put and thank you again. 🥳
