Search Unity

Question Why is my while loop infinite

Discussion in 'Editor & General Support' started by DonThomasDeLaVega, Aug 18, 2020.

  1. DonThomasDeLaVega

    DonThomasDeLaVega

    Joined:
    Feb 5, 2019
    Posts:
    41
    Hi everyone! So basically, i have a while loop:
    Code (CSharp):
    1. j = (CubzTarg.CubzSettings[CubzTarg.cubzSelected].OnCase); //This value is picked between 0 and 48 depending on what i do in runtime, doesn't matter
    2.             Debug.Log(j);
    3.             while(j != 0 || j != 7 || j != 14 || j != 21 || j != 28 || j != 35 || j!= 42){
    4.                 j-=1;
    5.                 Debug.Log(j);
    6.             }
    7.             Debug.Log(j);
    And i really don't understand why my loop is infinite. It was not even taken in count first, but then i decided to put all the possible cases separately, just so i'm clearer, but it's still not working; AND (brace yourselves) I've got none of the debug output that are showing. Not even the one that's before my presumed infinite while loop that makes my unity crash.
    here's the whole fonction, that i modified just a bit, just to verify some things, but it's still not working and only debug.
    Code (CSharp):
    1.     public void ChoseLin(){
    2.             j = (CubzTarg.CubzSettings[CubzTarg.cubzSelected].OnCase);
    3.             Debug.Log(j);
    4.             while(j != 0 || j != 7 || j != 14 || j != 21 || j != 28 || j != 35 || j!= 42){
    5.                 j = j-1;
    6.                 Debug.Log(j);
    7.             }
    8.             Debug.Log(j);
    9.             for(int i = j; i < j+7; i++ ){
    10.                 if(CaseTarg.CaseSettings[i].cubzOn != null){
    11.                     CaseTarg.CaseSettings[i].cubzOn.GetComponent<Kubzz>().revealed = true;
    12.                     Destroy(CaseTarg.CaseSettings[i].cubzOn, (2+(i/10)));
    13.             }
    14.         }
    15.         ChoiceRay.SetActive(false);
    16.     }
    This is a function called when i click on a button.
    By the way, not really related but i wanted to apologize because i've been posting a lot of my problems recently (At least one problem for each time i tried to get some things done in my game)
    Thank you, Thomas.
     
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Bench-test your while loop condition.

    If it is 7, then you have while(true || false || true || true etc), so the while loop continues
    If it is 6, then you have while(true || true|| true || true etc) so the while loop continues
    If it is 0, then you have while(false || true|| true || true etc) so the while loop continues
    If it is -1, then you have while(true || true|| true || true etc) so the while loop continues.

    There doesn't actually exist any value j could be that could cause the while loop to terminate.

    You can see this more clearly if you invert your condition (negation of conjunction):

    Code (CSharp):
    1.             while(!(j == 0 && j == 7 && j == 14 && j == 21 && j == 28 && j == 35 && j == 42)){
    2.                 j = j-1;
    3.                 Debug.Log(j);
    4.             }
    Since j can't be several numbers at once, the inner condition in the brackets will always be false, which is negated to always be true, and then you have an infinite loop.

    What I assume you want to do is do is round down j to the nearest multiple of 7. In which case, don't bother with a while loop at all, and just do something like

    j -= j % 7;
     
    Last edited: Aug 18, 2020
    Vryken and PraetorBlue like this.
  3. DonThomasDeLaVega

    DonThomasDeLaVega

    Joined:
    Feb 5, 2019
    Posts:
    41
    I searched for that! Thank you! i didn't find anything, so i made some homemade code. i changed that, and it works perfectly, even better than what i expected, because turns out the rest of my code i couldn't test was right too. A large leap for science, thanks!