.. | ||
Challenge | ||
main.py | ||
README.md |
6 - While Loops, Break/Continue, and Loop Translation
I just learned about For Loops, what the heck is a While Loop?
For Loops are great when you "know" the number of times you need to repeat something in Python (or any language). You have a finite number of steps that need to be repeated, so you do those things a certain number of times and then you're done.
While loops, on the other hand, repeat indefinitely until something is no longer true. A While Loop could go on "forever" if you let it (although generally this is a bad thing, more on that later).
While Loops are structured like this:
while someConditionIsTrue:
DO STUFF
Each time you run through the While Loop, the condition at the top is re-checked to see if it's still true, if it is, the code runs again, and again, and again, until that condition at the top is False, at which point, the while loop exits, and the code is no longer repeated.
What are we breaking or continuing?
Break and Continue are keywords that allow us to change the behavior of loops in our programs under certain circumstances we define, usually with if statements.
Continue can be used to jump back to the top of the loop. For For Loops, this means going back to the top, incrementing our index value (as defined by our call to range()) and then making sure that we still numbers to get through. For example, if you had a list of numbers 0, 1, 2, 3, 4 and the following code:
for x in range(5):
if x == 3:
continue
print(x)
The above code would print the following:
- 0
- 1
- 2
- 4
Three never prints, because once we hit that continue statement, we go right back to the top and onto the next number in the list of numbers that was generated by range(5)
Break on the other hand, when you hit a break statement, the loop ends, immediately, regardless of whether or not there were more things to do. Using the same list 0, 1, 2, 3, 4, and slightly different code:
for x in range(5):
if x == 3:
break
print(x)
We would get
- 0
- 1
- 2
Continue and break can also be used with While Loops, it all depends on what you're trying to accomplish.
What is Loop Translation?
Loop Translation is the idea that, a For Loop can always be written as a While Loop, and a While Loop can always be written as a For Loop. Loops in general do the same things once you break them down into there component parts and start running them on a CPU, so from that frame of reference it's easy to see how Loop Translation could be possible.
In general though, it's not good to "use the wrong type of loop" if you can help it. For loops are great when you know how many times you want to do something, but are terrible if the number of times you need to do something will change while the loop is running. While loops, on the other hand are great at doing things indefinitely, until some condition is no longer met, but the code needed to make a while loop do a certain number of things a certain number of times can be really ugly.
You as the programmer will need to decide when to use one loop or the other. This comes with experience, and for the most part is easy to pick up once you've made the decision a few times.
I've seen two loops now, are there other types?
Yes, sort of. Most other types of loops are based on For and While, the most common forms in my opinion are what I've shown you in this lesson and the last one.
There is a form of looping that can be done using something called Recursion. This however, goes way beyond the scope of these lessons, and so it isn't discussed here.