anonymous
  • anonymous
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?
edX CS50x Intro to CS I
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
e.mccormick
  • 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
anonymous
  • anonymous
Thanks. So is the difference in the way they are displayed just an aesthetic quirk of python?
e.mccormick
  • 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.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

e.mccormick
  • e.mccormick
Not sure why they say the putput would be different....
1 Attachment
anonymous
  • anonymous
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!
anonymous
  • anonymous
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.
rsmith6559
  • 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.
anonymous
  • anonymous
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?
e.mccormick
  • 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.
anonymous
  • anonymous
the deference is counter in for counter is used in the for bracket and while loop use the counter at the end

Looking for something else?

Not the answer you are looking for? Search for more explanations.