The Student Room Group

Why is this "Not a Statement" error showing? (Java)

I don't understand why this "Not a Statement" error has come up in my code?

not a statement error.png
In other languages you tend to have statements
if
else if
else

if is conditional
else if is conditional
else is not usually conditional, but youve added a condition. That may be the error, but Im not sure because I dont code in java
Reply 2
Original post by squirrology
I don't understand why this "Not a Statement" error has come up in my code?

not a statement error.png


Daniel00 is right. If wishing to evaluate a condition with else, the correct syntax would be




else if (P) {

}





where P is the condition you wish to evaluate.

In Java, if using else rather than else if, you never give it a condition. It is always used to say "Okay, none of the other conditions held, so execute this block to cover the remaining conditions my code hasn't explicitly considered yet".

That is, your typical block of if statements looks like this:






if (P1) {
...
} else if (P2) {
...
} else if (P3) {
...
...
} else if (Pn) {
...
} else {
...
}





Where the Pj are boolean conditions to evaluate. The else block executes only if none of the Pj were true.
(edited 6 years ago)
Original post by Daniel00
In other languages you tend to have statements
if
else if
else

if is conditional
else if is conditional
else is not usually conditional, but youve added a condition. That may be the error, but Im not sure because I dont code in java


Original post by Jarred
Daniel00 is right. If wishing to evaluate a condition with else, the correct syntax would be





else if (P) {

}






where P is the condition you wish to evaluate.

In Java, if using else rather than else if, you never give it a condition. It is always used to say "Okay, none of the other conditions held, so execute this block to cover the remaining conditions my code hasn't explicitly considered yet".

That is, your typical block of if statements looks like this:







if (P1) {
...
} else if (P2) {
...
} else if (P3) {
...
...
} else if (Pn) {
...
} else {
...
}






Where the Pj are boolean conditions to evaluate. The else block executes only if none of the Pj were true.

Thank you all so much for the explanation!!! :smile:

Quick Reply

Latest

Trending

Trending