Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

nfsiddiqui Group Title

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?

  • one year ago
  • one year ago

  • This Question is Open
  1. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    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

    • one year ago
  2. nfsiddiqui Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  3. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    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.

    • one year ago
  4. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

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

    • one year ago
    1 Attachment
  5. ado_kh Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

    • one year ago
  6. catherine Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
  7. rsmith6559 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
  8. sahilpatel Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

    • one year ago
  9. e.mccormick Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    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 months ago
  10. zakiullah2 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • 9 months ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.