0

I'm writing a program and my if statement doesn't give the expected output. I searched and found a tutorial on java if statements and copied the code on the tutorial into eclipse.

    int value = 1
    if (value == 3); {
    System.out.println("hello world")
    }

The problem is whatever I change the value to, the system still outputs "hello world". It doesn't make much sense to me I've done much more complicated programs in the past. Is there something stupid that I'm missing? Thanks Brad. ,

3 Answers 3

2

The semicolon in line 2 terminates the if and it should be removed

int value = 1;
if (value == 3) {
    System.out.println("hello world");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Bala R I've been struggling with this for a while now. Works perfectly. Do you guys mark threads "solved" on this forum and if so how?
1

You need to fix your formatting.

int value = 1;
if (value == 3); // extra ;
// totally unrelated block of code.
{
   System.out.println("hello world")
}

Which is why I use me IDE to do my formatting.

2 Comments

Missing semi after int value = 1
Cheers, took that from the OP.
0

In addition to the other answers, this code will not compile because you're missing a semicolon when doing variable assignment:

int value = 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.