none
Nested While loop not looping RRS feed

  • Question

  • Hi, I have a problem with a nested While loop. The outer loop is not looping back when the condition is satisfied. The program just stalls when it's "supposed" to loop back. I can break the loop no problem.

    Don't know why? and don't know how?

    http://smallbasic.com/program/?NPJ054

    I'd like to keep the outer loop as a While statement.

    It freezes up at the final position in the sample below.

    Any how and why help would be much appreciated. Thanks

    Sunday, April 7, 2013 11:01 PM
    Moderator

Answers

  • Sorry, I understand now. I have been able to reproduce your issue.

    I graduated the project to VB and used Visual Studio Intellitrace to step through your code. It seems that you are getting into an infinite loop during the second call to ShuflleDeck() (the For loop at line 44 never returns since the variable "i" is always set to value "1" by line 51).


    Please mark any answers and "vote as helpful" any posts that help you!

    Monday, April 8, 2013 1:14 AM
    Answerer
  • Goto "My settings" below your profile.

    There you can set your signature


    Jan [ WhTurner ] The Netherlands

    Monday, April 8, 2013 9:50 AM
    Answerer
  • I tried your program with LDDebug and found:

    the second time around the array "val" is completely filled and the If in line 51 is always "True". So i is never increased anymore. If you start again you should clear the array "val" by inserting:

    val=""

    The program then will run a second time.


    Jan [ WhTurner ] The Netherlands


    Monday, April 8, 2013 1:55 PM
    Answerer

All replies

  • When I press the Y key and press enter, the program iterates through the while loop again as expected (in both the browser and on the desktop.) Does this not work for you in both the browser and on the desktop?


    Please mark any answers and "vote as helpful" any posts that help you!

    Monday, April 8, 2013 12:04 AM
    Answerer
  • Thanks for checking gungan.

    No it doesn't. The inner loop works fine.

    But the outer loop just freezes. Are sure you checked the outer loop.

    Inner loop asks: deal another card? y  <----works fine

    Outer loop asks: Play again? y   <------ this one freezes


    Monday, April 8, 2013 12:20 AM
    Moderator
  • Sorry, I understand now. I have been able to reproduce your issue.

    I graduated the project to VB and used Visual Studio Intellitrace to step through your code. It seems that you are getting into an infinite loop during the second call to ShuflleDeck() (the For loop at line 44 never returns since the variable "i" is always set to value "1" by line 51).


    Please mark any answers and "vote as helpful" any posts that help you!

    Monday, April 8, 2013 1:14 AM
    Answerer
  • Thanks gungan.

    You're spot on. I debugged/isolated the call myself (just before checking this thread) but would've had no hope in isolating the infinite loop. My first venture into debugging. I'll fix the bug.

    It's weird how the inside loop runs after the call sub but not the outer. Could be some parallel running thing, like with events. I'll avoid making infinite loops from now on.

    Hey, if you come back to this thread, I've been wondering how do you put that footer on your posts?

    i.e. "Please mark any answers and "vote as helpful" any posts that help you!"



    Monday, April 8, 2013 3:21 AM
    Moderator
  • Goto "My settings" below your profile.

    There you can set your signature


    Jan [ WhTurner ] The Netherlands

    Monday, April 8, 2013 9:50 AM
    Answerer
  • Thanks Jan
    Monday, April 8, 2013 10:11 AM
    Moderator
  • Oh no. I've still got the same problem.

    re reply:"It seems that you are getting into an infinite loop during the second call to ShuflleDeck() (the For loop at line 44 never returns since the variable "i" is always set to value "1" by line 51)."

    I rewrote the ShuffleDeck() routine and now i = 53 thus breaking the loop. I don't see how the While loop can break and this routine still be an infinite loop??

    At line 7 I included TextWindow.WriteLine("stops after here on 2nd run") 'debug

    http://smallbasic.com/program/?HTX435

    What can I do to get this program to run?

    Monday, April 8, 2013 1:22 PM
    Moderator
  • I tried your program with LDDebug and found:

    the second time around the array "val" is completely filled and the If in line 51 is always "True". So i is never increased anymore. If you start again you should clear the array "val" by inserting:

    val=""

    The program then will run a second time.


    Jan [ WhTurner ] The Netherlands


    Monday, April 8, 2013 1:55 PM
    Answerer
  • Thanks Jan

     I don't think I ever would've thought of that.

    Monday, April 8, 2013 2:51 PM
    Moderator