infinite loop when adding item to list???

Hi everyone, I am currently getting an infinite loop (or at least I believe it is an infinite loop) when I run the following code:

for (int x = 0; x < NumberOfWallsInside; x = x + 1)   //This picks the gaps to be made (haven't been made yet)
        {
            Debug.Log(x);
            RandomYCoordinateOne = UnityEngine.Random.Range(1, ColumnLength);  //int min inclusive, int max exclusive
            RandomYCoordinateTwo = UnityEngine.Random.Range(1, ColumnLength);  //int min inclusive, int max exclusive
            if (RandomYCoordinateOne == RandomYCoordinateTwo || RandomYCoordinateOne == RandomYCoordinateTwo - 1 || RandomYCoordinateOne == RandomYCoordinateTwo + 1)
            {
                Debug.Log(x);
                x = x - 1;
            }
            else
            {
                ListOfYCoorindates.Add(5);
                ListOfYCoorindates.Add(RandomYCoordinateOne);
                ListOfYCoorindates.Add(RandomYCoordinateTwo);
            }
        }

What this is meant to do is choose two random points for each column of walls, check if these two random points are next to each other or euqal e.g. 4 and 5, 3 and 2 or 2 and 2…, if they are then the for loop needs to re-do these random points otherwise it adds it to the list ‘ListOfYCoordinates’ to be used later. From my tests using debug.Log(); I believe that the for loop works when I comment out the list.add(); that is inside the else statement, however I want this to become available so if u have any idea on whats wrong please help, please and thank you. Also, below is some more code about the variables in this for loop.

int RowLength = MapArray.GetLength(0);   //MapArray.GetLength is simply a constant
int ColumnLength = MapArray.GetLength(1);
int NumberOfWallsInside = RowLength - ((RowLength - 1)/2) - 2;
List<int> ListOfYCoorindates = new List<int>();
int RandomYCoordinateOne = 0;
int RandomYCoordinateTwo = 0;

Hi doublemax
In my opinion, the problem is in the decrementation x = x - 1, on line 9
because every time the condition is meet, you decrement x, and since you are not using an array that would rise an index out of range exception when the index is negative, you will end-up with a sort of infinite loop.

you confused the number of iterations and the number of the expected values, I would suggest you a while loop, and to put the coordinates into a structure,but I tried to keep the structure of your program bellow

for (int x = 0; x < NumberOfWallsInside;/*leave it blank*/)   
         {
             Debug.Log(x);
             RandomYCoordinateOne = Random.Range(1, ColumnLength);
             RandomYCoordinateTwo = Random.Range(1, ColumnLength);

             if (RandomYCoordinateOne == RandomYCoordinateTwo || RandomYCoordinateOne == RandomYCoordinateTwo - 1 || RandomYCoordinateOne == RandomYCoordinateTwo + 1)
             { // we didnt meet the expected coordinates so let try another time
                 Debug.Log(x);
                 //x = x - 1; you must do nothing here or continue
                 continue;
             }
             else
             {   // we have values that we can add to the list
                  x = x + 1;// the number of the good values found is incremented
                 ListOfYCoorindates.Add(5);
                 ListOfYCoorindates.Add(RandomYCoordinateOne);
                 ListOfYCoorindates.Add(RandomYCoordinateTwo);
             }
         }

here is better version

int found = 0;// the number of good coordinates 
while (found  < NumberOfWallsInside){

        RandomYCoordinateOne = Random.Range(1, ColumnLength);
        RandomYCoordinateTwo = Random.Range(1, ColumnLength);
        if (RandomYCoordinateOne == RandomYCoordinateTwo || 
             RandomYCoordinateOne == RandomYCoordinateTwo - 1 ||
             RandomYCoordinateOne == RandomYCoordinateTwo + 1)
       {
             continue;// try another time
       }else{
             found++;// found = found +1;
             ListOfYCoorindates.Add(5);
             ListOfYCoorindates.Add(RandomYCoordinateOne);
             ListOfYCoorindates.Add(RandomYCoordinateTwo);
       }
}

In a matter of general coding, you should never modify the iterator variable of a for loop inside the content of the loop. You have other loops that exist for this that would be more appropriate (while, do…while, …). For loops should always be used when you know, before starting the loop how many times the loop will be processed.
For loops should, therefore, never be infinite.

Moreover, try to avoid continue and break keywords, they break the semantic of the loop.

Apart from the philosophical answer, for your case, it is obviously the solution is to stop modifying the x variable that is used as the condition to end the loop.