## nfsiddiqui 2 years ago What is the difference in the way while and for loops execute? for i in range(0,10,1): print(i) This loop is exactly equivalent to: i = 0 while (i < 10): print(i) i += 1 the for loop prints 1 2 3 4 5 6 7 8 9 and the while loop prints 0123456789 Why does this difference occur? What is the difference in the way while and for loops are processed by the computer? Is it like 2+2=4 and 1+3=4? If so, why the difference in the way the output appears?

• This Question is Open
1. e.mccormick

While exists while a test condition is true. For exists for a specific range of events. while(evaluate this): do this for x in [1, 2, 3] do this

2. nfsiddiqui

Thanks. So is the difference in the way they are displayed just an aesthetic quirk of python?

3. e.mccormick

Sometimes a while loop comes in more handy because you are not sure how the evaluations will go and you need to continue until you reach a point. Other times, you just want to do one thing for each thing in a set, and then the for loop is better.

4. e.mccormick

Not sure why they say the putput would be different....

Hay, while and loops are very far from each others, you can get the difference by the experiance , but I will tell you without doing a bad tests ;). the difference is that while have no relation with "i" excpet doing the comparison, but for do the comparison and incremente the i in the same time. You're saying, and so, what is your -big- difference? I say that in loops execute you can't change the i (it's reserved by the loop), but you can do changing i in while execute, here an example: You can always do this: i = 0 while (i < 10) : { print(i) i += 1 i=5 } But you can never do this: for i in range(0,10,1): { print(i) i=5 } I think you get it, Chaww!

6. catherine

In a for loop, the incrementation is done automatically. In a while loop, if you want to increment a counter, you have to code it explicitly.

7. rsmith6559

for loops are essentially just a syntactically more convenient form of a while loop. The real difference in your original comparison is the range() function. range() creates an iterable object, and iterates over it. xrange() doesn't actually create an object, it just does the iterating. Try it with a 100 million iteration loop. That should run long enough so you can see a difference in the memory usage.

8. sahilpatel

Well, the lady asked why is the for loop printing a next line char after every print and the while loop jst printing the value of i in a single line without gaps or a new line char in between. even i m confused with it. but as i pasted the program on my machine and run it, the output was different. i pasted: for i in range(0,10,1): print(i) print #This loop is exactly equivalent to: i = 0 while (i < 10): print(i) i += 1 and output was: >>> ================================ RESTART ================================ >>> 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 >>> now, does it depend on the compiler version?? i use python 2.7.5 any explanations?

9. e.mccormick

I actually tried it in a couple languages... got the same results as you did. They were both continual lists or both one line. Never had one where it did one this way and the other another way.

10. zakiullah2

the deference is counter in for counter is used in the for bracket and while loop use the counter at the end