Jump to content

infinite loop

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
Hi. I've programmed or still programming a sudoku solver. It uses both loops and recursions... since it's using somewhat of a brute force I expect it to take it's time... I've tested it with many digits already filled in, but once I test it on some that are classed easy but don't have that much digids filled in, it's taking a lot of time. I am using Eclipse Galileo is there any way of knowing if program has entered an infinite loop which I suspect, in which case I should rewrite some parts of the program.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Maybe you get some useful info by printing the check statement of the for-loop or the statement of your while loop.
while(i<something){

  System.out.println(i+" < " + something + " == TRUE");

  ...

}

After a few seconds you probably got enough lines to see if it's making progress to reach an end, or if it's not.

#3
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
I'd put a global variable in (int i, for example) and then do a System.out.println("Recurse number"+i); i++; or something in there somewhere, like when it starts an iteration of the recursive method, and keep your java console open when you run it. Then you'll be able to watch how many times it prints to the console. If the number goes unrealistically high (I'm not sure what java's max value for an int is... You may get an OutOfBoundsException), then you've probably got an infinite loop.

That's just what I'd do, of course. I'm honestly not sure how to test to see if it goes on infinitely, but if it goes super high, then you'd likely want to re-think your code anyways, even if it's not infinite.
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)

#4
josep

josep

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
You can debug your code to check, or otherwise you can put some println statements to make sure where the application is doing something.

#5
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
Thanks. that actually helped... completly forgot the first rule of debugging a logical error:P(although I wasn't completely sure to begin with if it was an infinit loop or if the algorithm took that long) write some info out))) got the first info right though... was an int variable that jumped over a limit and never went in to an if-else that had a return in it... now everything works perfectly! Although I haven't programmed any GUI or haven't made any restrictions to the program what so ever, did it mainlty to see if I can write the program that does that, come up with an algorithm etc... anyone wants me to do a tutorial on that one? what it does is it takes 81 numbers to fill up the sudoku grid, (I've chosen 0s instead of empty spaces), solves it and writes out the completed sudoku.