7 Replies - 4164 Views - Last Post: 30 July 2010 - 12:49 PM Rate Topic: -----

#1 brianmen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 14-June 09

Simple While Loop Question

Posted 29 July 2010 - 04:35 PM

Hi all,

I am wondering if there is a way to exit the while loop before executing all of it...

For example:

stop=0
limit=5
while (stop < limit+1):
    print "one"
    stop=stop+1
    print "two"
    stop=stop+1
    print "three"
    stop=stop+1
    print "four"
    stop=stop+1
    print "five"
    stop=stop+1
    print "six"
    stop=stop+1
    stop=stop+1
    print "getting too close"
    print "gone too far"



gives me an output of:
one
two
three
four
five
six
getting too close
gone too far



How can I exit the while loop EXACTLY when "stop" and "limit" are equivalent in value?

Thanks for your help!

Is This A Good Question/Topic? 0
  • +

Replies To: Simple While Loop Question

#2 Guest_c.user*


Reputation:

Re: Simple While Loop Question

Posted 29 July 2010 - 06:20 PM

it has break
i = 0
while i < 10:
  print(i)
  i += 1
  if i == 3:
    break


This post has been edited by c.user: 29 July 2010 - 06:21 PM

Was This Post Helpful? 0

#3 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 697
  • Joined: 19-January 09

Re: Simple While Loop Question

Posted 30 July 2010 - 02:22 AM

If you are trying to exit it from with-in your code, then you can use the break like c.user said but if you want to stop the loop externally press ctrl+C and the script should stop. This is usefull to know when you get stuck in a loop.
Was This Post Helpful? 0
  • +
  • -

#4 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Simple While Loop Question

Posted 30 July 2010 - 03:19 AM

Quote

How can I exit the while loop EXACTLY when "stop" and "limit" are equivalent in value?


Maybe something like this...
while(stop != limit):
     #Your code


Was This Post Helpful? 0
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: Simple While Loop Question

Posted 30 July 2010 - 05:25 AM

This is a good question, but it alludes to more complicated issues that depend on the situation you are programming for.

Could you give a real-world example of what you are trying to accomplish?
Was This Post Helpful? 0
  • +
  • -

#6 brianmen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 14-June 09

Re: Simple While Loop Question

Posted 30 July 2010 - 09:15 AM

Thanks everyone,

Motoma, for a real world example, the only difference between my loop that I posted here and the loop I'm actually using is that some items are being added to a list. So rather than print statements, I'm using the
list.append("")
statement to put some padding into a list. When the condition in the while loop at top becomes false, I want to stop adding "" to my list.

View Postorangcacat, on 30 July 2010 - 02:19 AM, said:

Quote

How can I exit the while loop EXACTLY when "stop" and "limit" are equivalent in value?


Maybe something like this...
while(stop != limit):
     #Your code



Orangecat, that style of while loop is something I tried before my current scenario...

Doing

stop=0
limit=5
while (stop != limit):
    print "one"
    stop=stop+1
    print "two"
    stop=stop+1
    print "three"
    stop=stop+1
    print "four"
    stop=stop+1
    print "five"
    stop=stop+1
    print "six"
    stop=stop+1
    stop=stop+1
    print "getting too close"
    print "gone too far"



actually puts me into an infinite loop.... I feel like I'm missing something important here...

Thanks anyway for trying...
Was This Post Helpful? 0
  • +
  • -

#7 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: Simple While Loop Question

Posted 30 July 2010 - 11:17 AM

View Postbrianmen, on 30 July 2010 - 10:15 AM, said:

Motoma, for a real world example, the only difference between my loop that I posted here and the loop I'm actually using is that some items are being added to a list. So rather than print statements, I'm using the
list.append("")
statement to put some padding into a list. When the condition in the while loop at top becomes false, I want to stop adding "" to my list.


Individual circumstances lend themselves to individual solutions. For the loop you posted, my solution would be:
limit = 5
lookup = ['one', 'two', 'three', 'four', 'five', 'six']
for i in range(limit):
    print lookup[i]



But surely that's not what you want.

Another way to do this would be:
stop = 0
limit = 5
while stop > limit:
    if stop == 1: print "one"
    elif stop == 2: print "two"
    elif stop == 3: print "three"
    elif stop == 4: print "four"
    elif stop == 5: print "five"
    elif stop == 6: print "six"
    else: pring "Too far!"
    stop += 1



Still another solution would be:
stop=0
limit=5
while true:
    if stop = 0: print "one"
    stop += 1
    if stop > limit: break

    if stop = 0: print "two"
    stop += 1
    if stop > limit: break

    if stop = 0: print "three"
    stop += 1
    if stop > limit: break

    if stop = 0: print "four"
    stop += 1
    if stop > limit: break

    if stop = 0: print "five"
    stop += 1
    if stop > limit: break

    if stop = 0: print "six"
    stop += 1
    if stop > limit: break



In fact, there are a dozen other correct solutions to the problem, the right one to use depends on the actual problem. If you could post the code that you are having problems with, we would be able to guide you better.

Cheers,
Motoma
Was This Post Helpful? 0
  • +
  • -

#8 brianmen  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 14-June 09

Re: Simple While Loop Question

Posted 30 July 2010 - 12:49 PM

Motoma, those are some good solutions. I'll keep them in mind for the future. I was able to get what I needed done by using c.user's code as a model.

i = 0
while i < 10:
  print(i)
  i += 1
  if i == 3:
    break



It's a bit inefficient in practice, but it worked for me.

Thanks to everyone who helped me out. =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1