Need help understanding this while loop

Generally, the difference in the application between the two loops is that you use a while loop when you’re intending to run a process until a condition changes or until a condition is met.

Examples of using a while loop:

  1. Monitoring the temperature of a system. Shut system off when ambient temperature reaches 180 F.

Edit:
Make sure to design your loops carefully as you don’t want to inadverdently have your script stuck in an infinite while loop keeping other parts of your script from running. This is more susceptible to happen in a while loop versus in a for loop.

A for loop, on the other hand, is generally used to process a number of known items within a data structure (i.e., list, dictionary, tuple, set, etc.).

Examples include but are not limited to:

  1. Search a name within a list of strings. If found, break.
  2. Capitalize the first letter of every string in a list.
  3. Take the square of every number within a list.
  4. Call a set of functions in a dictionary.

Not sure if better, at least in my experience. As stated above, it has to do more with what you are attempting to accomplish in your script; that is, what is your objective.

Simple visualisation of while loop execution steps:

while_loop

Can a for loop do anything a while loop can do, and better?

In my opinion, No.

If one knows how many times the loop must be ‘looped’ . . then a For loop is ideal.

But, if one needs to, say, check every record in a file but one does not know how many records are in said file, then a While loop is just the ticket.

Again, for a known number of times to loop, the For. For an Un-Known number of loops, While.

And, in my opinion, programmers ought to know both the For and the While.
Josh.

| Paul onePythonUser
December 3 |

  • | - |

Josh:

I want to get back to this post. Is there ever a reason why I’d need to use a while loop instead of a for loop for this kind of iteration

Generally, the difference in the application between the two loops is that you use a while loop when you’re intending to run a process until a condition changes or until a condition is met.

A for loop, on the other hand, is generally used to process a number of known items within a data structure (i.e., list, dictionary, tuple, set, etc.).

Examples include but are not limited to:

  1. Search a name within a list of strings. If found, break.
  2. Capitalize the first letter of every string in a list.
  3. Take the square of every number within a list.
  4. Call a set of functions in a dictionary.

Josh:

Can a for loop do anything a while loop can do, and better?

Not sure if better, at least in my experience. As stated above, it has to do more with what you are attempting to accomplish in your script; that is, what is your objective.

In Python, for loops are not just about a known number of items, because it iterates over collections. You can, for example, iterate over the records in a file.

As you said:

A while loop is for doing something while a condition is true; a for loop is for iterating over a collection (list, set, range, etc).

It doesn’t necessarily need to be known, but the for loop is fundamentally about using the iterator protocol. For example,

for x in thing:
    ...

is basically shorthand for

itr = iter(thing)
while True:
    try:
        x = next(itr)
    except StopIteration:
        break
    ...
del itr

The for loop takes care of getting the iterator for the iterable thing, calling next, and handling the StopIteration exception to terminate the loop.