944,057 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4179
  • Python RSS
Nov 10th, 2006
0

Endless Loops

Expand Post »
I keep reading about endless loops, is that something you want or do you always have to control an endless loop situation?
Similar Threads
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Nov 11th, 2006
0

Re: Endless Loops

Click to Expand / Collapse  Quote originally posted by sneekula ...
I keep reading about endless loops, is that something you want or do you always have to control an endless loop situation?
Your operating system runs in an endless loop.

Truly endless loops are obviously bad news (i.e., shut off the computer and restart). However, most "endless loops" usually provide a way to break out. Here is a common and useful variety:

[php]
while True:
name = raw_input("What is your name? ")
if name == "King Arthur of Camelot":
break
[/php]
In this endless loop, the right response will get you over the bridge of death ... I mean, out of the loop.

This is called a "test-last" loop, because the test comes at the end of the loop. In many languages, it would be implemented as a "do ... until" type of construct. The point of test-lasting is to make sure that the body of the loop is executed at least once (i.e., the question gets asked at least once).

Others might implement the same code as

[php]
name = ""
while name != "King Arthur of Camelot":
name = raw_input("What is your name? ")
[/php]
but I like the first version.


Here's another example: a shell

[php]
dir = "C:"
while True:
command = raw_input(dir + "> ")
if command.startswith("dir"):
list_dir(command[3:]) # this would be parsed differently in real life!
elif command.startswith("cd"):
dir = change_dir(command[2:])
elif command == "exit":
break
else:
print "command not found"
[/php]
This shell format is the basis for text games, command-line interpreters, etc.

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Nov 12th, 2006
0

Re: Endless Loops

I see, so using:
Python Syntax (Toggle Plain Text)
  1. while True:
  2. pass
would be an endless loop, and you better give it a condition to break out of this loop. Something like:
Python Syntax (Toggle Plain Text)
  1. while True:
  2. quit = raw_input("press q to quit ")
  3. if quit == 'q':
  4. break
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Nov 12th, 2006
0

Re: Endless Loops

Accidental endless loops are the once you have to watch out for. The 'while loop' is more prone to this either through faulty escape logic or accidental use of the control variable:
[php]# accidental endless loop

# this is okay in Python
for k in range(10):
k = 2
print k

# but not in C ...
"""
// accidental endless loop in C

#include <stdio.h>

int main()
{
int k;

for(k = 0; k < 10; k++)
{
k = 2;
printf("%d", k);
}

return 0;
}
"""

# the C 'for loop' behaves more like this
# now you have accidental endless loop
k = 0
while k < 10:
k = 2
print k
[/php]
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Nov 12th, 2006
0

Re: Endless Loops

Even if you don't forget to put the counter into the 'while loop' you can still make an error ...
[php]# now you have an accidental endless loop
k = 0
while k < 10:
# the k counter
k += 1
print k
# more code here
# now comes the mistake
# k will never got to 10
k = 2
[/php]
Last edited by vegaseat; Nov 12th, 2006 at 3:56 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 12th, 2006
0

Re: Endless Loops

Just to add to Vega's example, I try to make my while conditions be "region" conditions instead of "boundary" conditions. Consider the difference between the two following innocent-looking pieces of code:

[php]
def print3rds(n): # Prints every third number from n up to 99

while n != 99:
print n,
n += 3
[/php]
vs.

[php]
def print3rds(n):

while n <= 99:
print n,
n += 3
[/php]
In the first code, the condition checks the "boundary" and quits if n hits the boundary. But of course, if print3rds(4) is called, n will *never* be equal to the boundary. Hence, an endless loop!

In the second code, the condition checks the "region" and quits if n exits the valid region. In general, this is a much safer way to write conditions.

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Nov 14th, 2006
0

Re: Endless Loops

When you do GUI programming, you will find another endless loop usually called mainloop(). This is the loop that continuously scans for windows events like mouse clicks, mouse position, button clicks, key presses and so on.

The mainloop() is not truely endless, when you click on the little x on the upper right corner of the window frame it quits.
Last edited by bumsfeld; Nov 14th, 2006 at 1:01 pm.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Qestion on Encoding and Decoding.
Next Thread in Python Forum Timeline: Python Byte Code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC