Using the 'continue' keyword.

Sep 3, 2011 at 1:30pm
I've been told that I should always avoid using continue to avoid creating spaghetti code. Are there situations where it is appropriate?

For fun I've been trying to recreate the classic minesweeper game. When the user selects a cell, that cell is revealed. If the cell is not adjacent to any mines, it will also reveal the cells around it. I've written a recursive function to emulate this. In the function I use a continue to skip revealing the current cell that is already being revealed.

If I wanted to remove the continue, how would I do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void reveal_cell(int x, int y)
{
	if (grid[x][y].hidden == false) return;	//Avoid doing touching a cell that's been revealed.
	grid[x][y].hidden = false; //Reveal the cell
	
	//Reveal adjascent cells.
	if (grid[x][y].num_adjascent_mines == 0)
	{
		for (int i = x-1; i <= x+1; i++)
		{
			for (int j = y-1; j <= y+1; j++)
			{
				if (i==x && j==y) continue;  //Skip the current cell.
				reveal_cell(i,j);
			}
		}
	}
}


Notes about the code:
-grid is a global structure representing each cell on the grid. I think the elements of it that I use in this function are self-explanatory and are initialized elsewhere.
-I understand that making grid a structure (not a class) and global is probably not the best way, but I'm not great yet and I'll get to that later.
-I do some limiting to ensure that the function does not go off of the edge of the grid. That is not relevant to this question so I have removed it to simplify what I've presented.

Last edited on Sep 3, 2011 at 1:31pm
Sep 3, 2011 at 1:41pm
you just want to continue onto the next iteration if the condition (i==x && j==y) is true. This will happen anyway if you just don't call reveal_cell in this scenario.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void reveal_cell(int x, int y)
{
	if (grid[x][y].hidden == false) return;	//Avoid doing touching a cell that's been revealed.
	grid[x][y].hidden = false; //Reveal the cell
	
	//Reveal adjascent cells.
	if (grid[x][y].num_adjascent_mines == 0)
	{
		for (int i = x-1; i <= x+1; i++)
		{
			for (int j = y-1; j <= y+1; j++)
			{
				if ( ! ( i==x && j==y ) ) 
     			            reveal_cell(i,j);
			}
		}
	}
}

Sep 3, 2011 at 1:48pm
Here is another example of code where I use continues in the same program. This code is called when I am creating the grid. It goes through each cell in the grid and count the number of adjascent mines for each cell. The number of rows and columns on the grid is stored in Ymax and Xmax.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Count adjascant cells
for (int j1=0; j1 < Ymax; j1++)  //Go through all rows
{
	for (int i1=0; i1 < Xmax; i1++)  //Go through all columns
	{
		for (int j2=j1-1; j2<=j1+1; j2++) //Compare with adjascent rows
		{
			for (int i2=i1-1; i2 <= i1+1; i2++) //Compare with adjascent columns
			{
				if (i2==i1 && j2==j1) continue; //Don't include current cell
				if (i2 < 0 || j2 < 0 || i2 >= Xmax || j2 >= Ymax) continue; //Don't go off the grid.
				if (grid[i2][j2].mine) grid[i1][j1].num_adjascent_mines++;
			}
		}
	}
}
Sep 3, 2011 at 1:54pm
So some know-it-all-wannabe told you an "always/never" rule without knowing anything about your code?

Most people today have never actually seen true Spaghetti code. You cannot use continue to make it -- at least not without an extreme amount of creativity.

The continue exists for just the kind of situation you are using... though, that said, you would probably do better just to negate your conditions as quirkyusername suggests.

Hope this helps.
Sep 3, 2011 at 2:00pm
Thanks Quirky and Duoas. The guy who told me that was a colleague at work who is in charge of our coding standards and who is our C++ guru. He tells me he can go for months without writing a continue.

So as long as the code is clean and obvious, I wont go to excess to remove my continues.
Sep 3, 2011 at 3:01pm
Continue like any other keyword is just a tool that you have at your disposal and should use when it's appropriate. Most features probably wouldn't be part of the language if they weren't useful.
Topic archived. No new replies allowed.