Converting for loops into while loop [closed]

You are missing the point of for and while loops, this question does a good job of explaining it.

Basically, a for loops iterate through the list and you can operate on the items as it does so.

In contrast, while loop is used to run until a condition is met, such as a flag being triggered.

The for loop:

mylist = ["this", "is", "a", "for", "loop"]
for element in mylist:
    print(element)

Returns:

this
is
a
while
loop

Where as this while loop:

count = 0
while count != 10:
    print(count)
    count += 1
print("count has reached 10")

Returns:

0
1
2
3
4
5
6
7
8
9
count has reached 10

In summary, a for loop is used to iterate through an array or generator object, where as a while loop is used to run until a condition is met.

Leave a Comment