else can not have a condition. It is executed when the previous condition is false.
1 2 3
else (x == 100) // makes no sense
elseif(x == 100) // makes sense, but still wrong (see below)
The 2nd line there will solve the compiler error, but will not print when x is equal to 100, because it is trumped by an earlier if statement.
Here's how it'd work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int x = 100;
if(x > 0) // 100 is > 0, so this is true
{
// this code will be executed
// and the following else statement(s) will not be
}
elseif(x < 0)
{
// this is skipped because previous if() was true
}
elseif(x == 100)
{
// this is also skipped because previous if() was true
// it doesn't matter whether or not x==100 is true
// because the 'else' makes it skip over the condition entirely.
}
If you want to have a x==100 condition, you either need to remove the else (in which case BOTH the x>0 and x==100 code will execute), or you need to move the x==100 condition so that it's earlier in the chain and has a higher priority.
hello,i have some sort of question,y now Disch is the real pro between us,but i think that is this situations where you have more conditions,you can put the most important,or the one in a million shot (if i can say that) condition first..so that you verify the first time if (in you're case) x==100.because if x==100 it does not matter if it is positive or negative.from 0 to 100 there are many positive numbers,but only one is unique..hope thismethod is good.