Turn on thread page Beta
    • Community Assistant
    • Very Important Poster
    • Thread Starter
    Offline

    20
    ReputationRep:
    Community Assistant
    Very Important Poster
    I don't understand why this "Not a Statement" error has come up in my code?

    Name:  not a statement error.png
Views: 37
Size:  13.9 KB
    Offline

    12
    ReputationRep:
    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
    Posted on the TSR App. Download from Apple or Google Play
    • Wiki Support Team
    Offline

    18
    ReputationRep:
    Wiki Support Team
    (Original post by squirrology)
    I don't understand why this "Not a Statement" error has come up in my code?

    Name:  not a statement error.png
Views: 37
Size:  13.9 KB
    Daniel00 is right. If wishing to evaluate a condition with else, the correct syntax would be
    Code:
    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:

    Code:
    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.
    • Community Assistant
    • Very Important Poster
    • Thread Starter
    Offline

    20
    ReputationRep:
    Community Assistant
    Very Important Poster
    (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
    Code:
    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:

    Code:
    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!!!
    • Community Assistant
    Offline

    20
    ReputationRep:
    Community Assistant
    You also need to be careful with how you are trying to evaluate your conditions. Your if statement is evaluating

    Code:
    e.getSource() == redButton
    while your else (would be else if) is evaluating

    Code:
    blueButton == e.getSource()
    At best this might be seen as bad practice, at worst your code won't work properly. Stick to keeping things in the same order, the generally accepted way would be e.getSource() on the left.
 
 
 

University open days

  1. Norwich University of the Arts
    Postgraduate Open Days Postgraduate
    Thu, 19 Jul '18
  2. University of Sunderland
    Postgraduate Open Day Postgraduate
    Thu, 19 Jul '18
  3. Plymouth College of Art
    All MA Programmes Postgraduate
    Thu, 19 Jul '18
Poll
How are you feeling in the run-up to Results Day 2018?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.