finding errors in if statements

Find the errors in the following if statements.

a. if (x && y == 0) { x = 1; y = 1; }

b. if (1 <= x <= 10)

System.out.println(x);

c. if (!s.equals("nickels") || !s.equals("pennies")

|| !s.equals("dimes") || !s.equals("quarters"))

System.out.print("Input error!");

d. if (input.equalsIgnoreCase("N") || "NO")

return;

To some advanced users, this may seem simple. However for me it;s hard to pick out! Does anyone want to take a wack at it? Thanks!

Re: finding errors in if statements

Quote:

Originally Posted by

**JMAsterson** Find the errors in the following if statements.

a. if (x && y == 0) { x = 1; y = 1; }

b. if (1 <= x <= 10)

System.out.println(x);

c. if (!s.equals("nickels") || !s.equals("pennies")

|| !s.equals("dimes") || !s.equals("quarters"))

System.out.print("Input error!");

d. if (input.equalsIgnoreCase("N") || "NO")

return;

To some advanced users, this may seem simple. However for me it;s hard to pick out! Does anyone want to take a wack at it? Thanks!

The first person who should publish a "whack at it" in this thread is you.

Please elevate this thread out of the realm of "homework dump" and into the realm of "collaborative effort".

Re: finding errors in if statements

Ok, not sure if this is right. I think in b.) it should be changed to f (1 <= x && x <= 10) otherwise it wont work.

In c.) ! means not, so I dont understand what !s.equals("dimes") etc means? that looks like it would translate to not s equals.

a.) doesnt make much sense to me either because what i'm getting from it is : if x and y have a value of 0, x =1 and y=1.

Re: finding errors in if statements

Can someone tell me if my answer (or part of the answer) if true at all? Thanks.

Re: finding errors in if statements

c) is also incorrect but for an entirely different reason (the others don't even syntactically make sense); suppose you want to test something against A and B; suppose that something is C; of course it isn't equals to A or B; but now suppose the something if A; it is definitely equal to A but it isn't equal to B so the or test (||) is still true: A isn't equals to A or B.

kind regards,

Jos (or not Jos)

Re: finding errors in if statements

Oh, i see, yes that makes sense. So are you saying my other answers are wrong? Not sure where else to go from here with that.