Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Loops are very useful when needing to do the same action multiple time (iterations). For loops are usually better when the amount of iterations is known and while loops are usually better when the number of iterations is not known.
However, you can pretty much do any process (algorithm) with either style of loop. You can also use recursion.
Iterate through every char in a string
Indexes are used to keep track of what we're working with for each iteration. With while loops, you have to manually change the index. Be careful, infinite loops are created when a terminating condition is never met. IE the index never changes, a count never changes, or whatever condition we decide for the loop.
In other languages, there are do while loops. However, Python doesn't use them without some extra effort.
You can also use the range() function to set a start, stop, and step for your for loops. This is similar to the start, stop, and step of string slicing.
Here is a nifty way to demonstrate Multiplication and Division as loops of Addition and Subtraction!