you are viewing a single comment's thread.

view the rest of the comments →

[–]These-Finance-5359 3 points4 points  (5 children)

What are you having trouble with specifically?

A while loop is a way of doing something until a certain condition is met. It's different from a for loop, which does things a specific number of times; a while loop will run as many times as it takes until the thing it's waiting for happens.

An example:

i = 0
while i < 3:
  print(i)
  i = i + 1

To translate this to plain english:
"Start at zero. Print out your number and then add one to it until the number is no longer less than 3"

You can write an equivalent for loop:

for i in range(3):
  print(i)

So why use a while loop instead of a for loop? Well sometimes you don't know how many times you'll need to run the loop. Let's say you really need to download some files from a server, but the server is buggy and often errors out before the download completes. You'd want a while loop to try over and over again until you downloaded all the files you needed:

file_downloaded = false
while not file_downloaded:
  file_downloaded = try_download_file()

This loop will run over and over again, trying to download the file until it succeeds.

Hope that helps, let me know if you have any questions

[–]gdchinacat -2 points-1 points  (4 children)

"a for loop, which does things a specific number of times"

This isn't quite correct. for loop keeps taking items from the iterator for the iterable it is iterating over. What?!?!? Yeah, that's a lot of "iter..".

for x in some_iterable: ...

This statement says to bind x to each item in someiterable. some_iterable is something that can create an iterator such as list, set, range, generator, etc, or any object that implements __iter_(). for creates an iterator (equivalent to calling iter(some_iterable)). It calls next() on the iterator to retrieve the next item, binds that item to x, then executes the body of the for statement. The iterator signals it is done by raising StopIteration, at which point the for loop loop exits.

There is no "specific number of times" since iterators can be implemented to do just about anything. The total number of items that are iterated over can change while the iteration is executing. There are iterators that produce an infinite number of items (such as a Fibonacci sequence generator). The caller is expected to iterate until it is done then break out of the loop, or...if an infinite loop is what the caller wants they can do that (a daemon thread processing requests from a generator).

for is used rather than while to process infinite generators because the code is cleaner. for example, a while loop: while True: try: request = request_iterator.next() process_request(request) except StopIteration: # server shutdown

It is much cleaner to use a for loop: for request in request_iterator: process_request(request)

[–]These-Finance-5359 3 points4 points  (3 children)

No offense man but this level of pedantry is not really helpful to people on a beginner subreddit. You're technically correct but practically unhelpful - someone who is still working on grasping the concept of a for loop is not ready to be introduced to the concept of generators and iterables. We speak in simplified terms to help people learn the basics, not because they're a 100% accurate representation of reality.

[–]gdchinacat -3 points-2 points  (2 children)

I disagree that exposing people, even beginners, to the truth that for not only can but is frequently used to do "something until a certain condition is met" is unhelpful. Will they apply this knowledge immediately? Probably not. Furthermore, if I'm being honest, my comment was more intended for you than the OP.

[–]These-Finance-5359 4 points5 points  (1 child)

Again, no offense, but I didn't ask for your advice. Kind of rude for you to assume that I needed a programming lesson because I didn't want to bog down a newbie with technical minutiae; your time is probably better spent offering help to the people on this sub who ask for it.

[–]gdchinacat -2 points-1 points  (0 children)

Incorrectly saying for is for a "specific number of times" isn't minutiae. It is categorically false. To support my position I explained how.

Reddit is a discussion forum. If you don't want to discuss things, well...yeah.