goto problem

I want to break only 2 for. Not third.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
	for (int z = 0; z != 100; z++)
	{
		for (int y = 0; y != 100; y++)
			for (int x = 0; x != 100; x++)
				if (y == 50) goto multibreak;
multibreak:
	}
	return 0;
}


Why this compiler output?

1.cpp: In function 'int main()':
1.cpp:9:2: error: expected primary-expression before '}' token
1.cpp:9:2: error: expected ';' before '}' token
Last edited on
I rarely use gotos, as they're evil, but I believe they must be associated with a statement. The compiler is complaining that there is no statement between your label and the closing brace. You can just follow its advice and supply a ;, which is a null statement.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	for (int z = 0; z != 100; z++)
	{
		for (int y = 0; y != 100; y++)
			for (int x = 0; x != 100; x++)
				if (y == 50) goto multibreak;
multibreak:
		; // null statement
	}
	return 0;
}


Andy

P.S. I don't get why you're testing y after you enter the x loop?
Last edited on
Yes, the ';' is the solution! Thanks!
The thing about null statement is tricky!

I use rarely gotos too. But when you must break many 'for' without extra tests, it is almost irreplaceable.

About PS: It is a small piece of code which reproduce the error.
Real code is different.
Topic archived. No new replies allowed.