Home > OS >  What's the exact distinction between the FOR loop and the WHILE Loop
What's the exact distinction between the FOR loop and the WHILE Loop

Time:12-13

I understand that loops are an integral part of any programming language. They help us to iterate tasks that are repetitive. But I don't seem to understand why these 2 loops are required seperately. Can't just one of them do the job? I have tried both loops and still can't seem to understand how are these 2 loops different and where it is better to use which loop ! Please someone explain to me in detail with example!

I have tried many programs like factorial, sum of a list and prime numbers, using both loops but I'm unable to understand where to use which loop because surely there must be some rule of thumb to decide where to use which loop ! I expect someone to clear this query of mine with example.

CodePudding user response:

There is usually not a question. If you need to run a fixed number of loops, or you need to do something once for each item in a collection, then you want a for loop. Anything based on a counter uses a for loop. If you need to loop until some specific condition happens, or loop forever, then you want a while.

# Print 5 numbers.
for i in range(5):
    print(i)

It's usually obvious when you've made the wrong choice:

i = 0
while i < 5:
    print(i)
    i  = 1

That's a lot of extra typing to do what the for loop does.

lst = [5,4,3,2,1]
for n in lst:
    print("This element is",n)

But here is a case where while is better:

while True:
    s = input("Type q to quit: ")
    if s == 'q':
        break

CodePudding user response:

For ends by itself when it comes to the end of iterable
While ends when its condition is False

if there is some condition you need to check on every loop you need to use While
if there is an iterable object you need to iterate on you've to use for

CodePudding user response:

The main distinction would be that it isn't necessary to specifically iterate the 'for loop' but if the 'while loop' isn't iterated, it becomes an infinite loop.

For example:

#In for loop n = 5 for i in range (n): print(i) #This program will print the numbers from 0 to 4

but if you do something similar with while loop like this:

n = 5 i = 0 while i in range (n): print(i)

then this will just print '0' and will be stuck in an infinite loop.

CodePudding user response:

In a computer programming language, iteration statements like for loop and while loop and more are used for repeated execution of the instruction in a program. Both for loop and while loop is used to execute the statements repeatedly while the program runs. The major difference between for loop and while loop is that for loop is used when the number of iterations is known, whereas execution is done in a while loop until the statement in the program is proved wrong. For loop also provide a concise way of writing the loop structure.

CodePudding user response:

A for loop is indeed not necessary. But most loops involving iterators are simpler to write using a for loop than an equivalent while loop.

https://github.com/brettcannon/desugar provides many examples of "desugaring" various Python constructs into equivalent, more "basic" constructs. for loops can be converted to while loops by manually iterating over the given iterable. The following example is from https://github.com/brettcannon/desugar#for-.

for a in b:
    c

becomes

# next and iter are built-in functions, so there's no concern
# that we're just hiding a for loop here somewhere.
_iter = iter(b)
while True:
    try:
        a = next(_iter)
    except StopIteration:
        break
    else:
        c
del _iter

The for loop automatically gets an iterator for the iterable, calls next, and handles StopIteration to terminate the loop.

Which would you prefer to write?

  • Related