Loading...
Logo
Processing Forum
In an attempt to demonstrate to my students how easy it is to get caught in an infinite loop, I ran the following code in Processing:

int count = 0;
while (count < 100) {
  println("hello world");
}

The problem is that once I'm caught in an infinite loop, there seems to be no way to get out without brute forcing Processing to quit.  Perhaps I'm missing something...is there a friendlier way to exit a program that is caught in an infinite loop?

Replies(7)

to my basic programming knowledge, a while loop also needs the counter to be updated while it's running.
So in your very succinct example you would want something like this.
Copy code
  1. int count =0;
  2. while (count<100){
  3.       println("hello world");
  4.       count ++;
  5. }
Depending on what your conditions and loops are about, i'd recommend looking at the break(), continue() and return() functions.


Hope this helps.
Sorry...yes, that fixes the infinite loop, but what I'm really concerned about is what to do when I find myself caught in such a loop?  Killing the program (such as with Ctrl-Alt-Del in Windows) will lose any unsaved work.  However, I can't seem to find a better way.  The environment becomes unresponsive with an infinite loop as simple as the one in my original post.

Maybe this is only with console output?
Maybe this is a bug that should be reported?
Maybe there is a button/option I don't know about that allows a safe escape?

I plan to teach a lesson in which students write loops to output text to the screen, but I'm thinking that I should have them do it in a different environment than Processing, since they will inevitably accidentally write infinite loops (happens to us all).  So, unless there is some workaround within Processing, I might give the students a quick intro to Dr. Java as a place to write and test loops with text output.
" that fixes the infinite loop, but what I'm really concerned about is what to do when I find myself caught in such a loop"
Then that's not a programming question... Moving to the proper forum section.


on windows 7, with processing 1.5.1 I can interrupt infinite loops by pressing the stop button. works with your code snippet as well.

(edit: the square within a circle, to the right of the play button, underneeth the edit menu)
Thank you for the replies (and for moving the question phi.lho...sorry, it arose as an issue related to programming, but I'm slowly learning the distinction among the forums).

This may be a hardware-specific issue, as I can reproduce the problem on some Windows and Mac environments, but was able to get it to work on another Windows machine (as was paul).

Thank you again.
No problem, my remarks are more for education than for shame... (not for shame at all, actually!)

Perhaps on some computers you have to insist on the stop button, if the sketch hogs the thread (CPU bound).
Note: if you must kill the process, you are likely to find the code in the temporary folder of your system, where Processing saves the .pde, processes it to .java, then to .class to run it. There was a thread on the forum about finding where this temp folder can be.
saving before running is a good habit to adopt.

i use ctrl-s ctrl-r to save and then run and it's second nature now.

proper source control is also a good idea, so you don't lose working versions with bad experiments. (svn, git, er, probably some windows things)