If-else statements, always running

I am working on making a coffee ordering system. It takes inputs from users and calculates the price of their coffee. One of the inputs asked for is "syrupshots". This code always makes syrupshots = 3 even if the user properly chose a number according to the size of their coffee.

//if size == tall syrup shots 3) < syrupshots = 3; >//else if size == medium syrup shots 5) < syrupshots = 3; >//else if size == Venti syrup shots 7) < syrupshots = 3; >System.out.println(syrupshots); 
I am not sure why syrupshots is always = 3 no matter what. asked Sep 30, 2015 at 16:11 JRYamasaki JRYamasaki Every condition evaluates to syrupshots = 3; . Commented Sep 30, 2015 at 16:13 it simply means that your size is equal to 1 and syrupshots in less than 1 Commented Sep 30, 2015 at 16:17

2 Answers 2

Not always, sometimes it returns 1 or 2. But never >3, like it looks like it should.

Your problem is with logical operator precedence. For example:

if(size == 1 && syrupshots < 1 || syrupshots >3) <> 

If size=3 and syrupshots=5 (a valid combination), then this if block is still entered because the && is evaluated first:

size == 1 && syrupshots < 1 

and that equals false , so you have left (false || syrupshots > 3)

BUT then the || is evaluated, syrupshots > 3 is true , so the whole expression is true

You need to change the order of precedence by using brackets:

if(size == 1 && (syrupshots < 1 || syrupshots >3)) <>