Now you might ask, "Why such a silly error message?" but it actually makes sense. Here's the explanation.
When you add a semicolon to a for loop like that, you're telling the compiler "Loop this many times, but do nothing else". It's effectively the same as
Code:
for (outer = 0; outer < 9; outer++) {}
accomplishing only moving outer from 0 up to 9 incrementally. In fact, if you looked at the compiler's output, it's quite possible the compiler optimized that line of code directly to the equivalent of:
So, by terminating that for loop in such a way, the braces you added simply setup a new scope
Code:
{
didSwap = 0;
for (inner = outer; inner < 10; inner++)
{
if (nums[inner] < nums[outer])
{
temp = nums[inner];
nums[inner] = nums[outer];
nums[outer] = temp;
didSwap = 1;
}
}
if (didSwap == 0)
{
break;
}
}
This scope obviously has no loop in it where the break statement is, and the error is generated.
By the way, if you turn up the warning levels in your compiler, you may get warnings for these types of errors (and expose other things you should fix):
Code:
cc nope.c -o nope
nope.c:7:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main()
^~~~
nope.c:42:13: error: 'break' statement not in loop or switch statement
break;
^
nope.c:25:40: warning: for loop has empty body [-Wempty-body]
for (outer = 0; outer < 9; outer++);
^
nope.c:25:40: note: put the semicolon on a separate line to silence this warning
2 warnings and 1 error generated.
make: *** [nope] Error 1