# How to break out of nested for loops

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);
``````
• What language? What pset number?
– Tim
Dec 28 '14 at 21:49
• pset 3.language c Dec 28 '14 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 Dec 28 '14 at 22:21
• Have you tried labeling the for loop and breaking it that way?
– Tim
Dec 28 '14 at 22:34

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);
``````
• it will not stop .for example if he finds the tile in board it will find it again in  because of the swap. Dec 29 '14 at 0:28
• anyway, thanks but i have to trace the blank tile otherwise its very dificult Dec 29 '14 at 0:29
• @pavlosromios if you want to break the loop 1 its is also possiple, i am going to update the answer Dec 29 '14 at 1:31
• sorry.now i noticed it.you claim i=n also with the break Dec 29 '14 at 9:31
• @pavlos if this answer solves your problem please click the small tick below the down arrow.
– Tim
Dec 30 '14 at 18:37