-1

I would like to stop only the nested for loop, not the do while loop, in the following code. I have tried all the possible positions of break without success.

do
{
    draw(d); 
    printf("tile to move\n");  
    int tile = GetInt();

    for(int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; ++j)
        {
            if(tile == board[i][j])
            {
                if(board[i+1][j] == 0)
                {
                    int f = tile;
                    board[i][j] = 0;
                    board[i+1][j] = f;
                }
            }
        }
    }
}
while(true);
4
  • What language? What pset number?
    – Tim
    Commented Dec 28, 2014 at 21:49
  • pset 3.language c Commented Dec 28, 2014 at 22:01
  • the structure of fifteen breaks the loop by returning true but im doing it step by step to understand it better.it seems my function move not to be working when i want to swap elements from the last raw Commented Dec 28, 2014 at 22:21
  • Have you tried labeling the for loop and breaking it that way?
    – Tim
    Commented Dec 28, 2014 at 22:34

1 Answer 1

2

I am not sure what your problem is, but you can call break at any time, here is an example:

do
{
    // For loop 1
    for (int i = 0; i < n; i++)
    {
        // For loop 2
        for(int k = 0; k < n; k++)
        {
            // When condition b you can break loop 1 and loop 2
            if (condition b)
            {
                i = n;
                break;
            }
        }
    }

} while(condition a);
5
  • it will not stop .for example if he finds the tile in board[2][3] it will find it again in [3][3] because of the swap. Commented Dec 29, 2014 at 0:28
  • anyway, thanks but i have to trace the blank tile otherwise its very dificult Commented Dec 29, 2014 at 0:29
  • @pavlosromios if you want to break the loop 1 its is also possiple, i am going to update the answer
    – wallek876
    Commented Dec 29, 2014 at 1:31
  • sorry.now i noticed it.you claim i=n also with the break Commented Dec 29, 2014 at 9:31
  • @pavlos if this answer solves your problem please click the small tick below the down arrow.
    – Tim
    Commented Dec 30, 2014 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .