1
public static void main(String args[]){
    int x = 0;
    if (x > 0) 
        System.out.println("positive ");
        System.out.println("zero ");
        System.out.println("negative"); 
}

output:

zero 
negative

Why does the program not print the positive but prints zero and negative?

2
  • Can we close this question? Commented Oct 20, 2015 at 18:39
  • If you would rewrite your code, it would look like if (x > 0) {System.out.println("positive ");} System.out.println("zero "); System.out.println("negative"); As you can see, last 2 prints don't go under If-clause Commented Oct 20, 2015 at 20:20

5 Answers 5

2

Just try with:

if (x > 0) {
    System.out.println("positive ");
} else if (x == 0) {
    System.out.println("zero ");
} else {
    System.out.println("negative");
}
Sign up to request clarification or add additional context in comments.

Comments

1

Why do you think that it will print positive when value of x is 0. x > 0 will return false so it will skip System.out.println("positive "); and it will print other 2 lines because your if block ended in System.out.println("positive "); line itself

Comments

0

Because you didn't put the brackets so the

 System.out.println("positive ");

is coming inside the If condition and obviously if condition is not true that's why it is not printing the positive.

Comments

0

Because x is set to 0 so the if condition is never verified and the "positive" statement is not executed

You probably wanted to write something like

if (x > 0)
    System.out.println("positive ");
if (x == 0)
    System.out.println("zero ");
if (x < 0)
    System.out.println("negative");

1 Comment

Not wrong, but the answer containing the else if / else is obviously a pinch better, since you can avoid 2 comparissions, if the first is matched.
0

It is not printing positive because it fails the if condition. The value of your x is 0 which is not greater than 0. It does print zero and negative because it falls outside of your if condtion due to the lack of curly braces. If no {} then only the first line after the if condition is executed.

In the off chance that you want your code to print all three i.e. positive negative and zero when your x > 0 (can't see why you would do that), then you are missing curly braces and your code should look like:

public static void main(String args[]){
    int x = 0;
    if (x > 0) 
    {
        System.out.println("positive ");
        System.out.println("zero ");
        System.out.println("negative"); 
    }
}

For your code, this will print nothing because it fails the if condition so nothing inside curly braces will be executed.

But if you want your program to print positive when >0, negative when <0 and 0 when =0 then @hsz's answer is the one your are looking for.

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.