An "else" must always be after an "if", directly!. "always" means you cannot just write an "else" clause by itself! And "directly" means, you cannot write some other code between the end of the "if" and beginning of the "else".
So where is the "if" for the "else" in line 10? It's not the one in line 3!
What you have written is following:
if (condition) action; // in your case, action is not there, so it's empty! The action cannot follow AFTER the semicolon!
{ // start of a code block
some code // this code is ALWAYS executed, independent of the condition!.
} // end of code block
else // where is my if-statement? there is only some stange code above me, but no "if"!
What you may not know are 2 cases that you combined:
1.)
- x = 7;
-
y = false;
-
if (y = (x==7)); // this is a legal statement. notice "=" and "==". It sets y to true. Always. Now the action of the if-statement is called. But this action is not given, so it does nothing!
-
x = 9; // this is alway set, regardless of the previous "if" statement
2.)
- x=7;
-
{ // legal. it's just the start of a code block. You could write "synchronized" in front if you like. It has nothing to do with an "if" statement.
-
y=10;
-
int z=20;
-
}
-
y=30; // just set y to 30 that was just set to 10 before.
-
// x has still old value = 7
-
// but you cannot access z anymore!. Z was only valid inside the code block. If you try to write "z=10;", it will not compile. (Scope!)