Can we use both `&&` and `||` in one expression?


#1

Question

Can we use both && and || in one expression?

Answer

You can. For example: age < 18 && parentSupervision || age >= 18. It is advisable to use () to group each of those statements because it can get a little confusing at times. This expression says: if age is less than 18 but parentSupervision is true, the expression is true. Otherwise if age is greater than or equal to 18, the expression is also true. && takes precedence over || when it comes to order, which is why it reads this way instead of: (age is less than 18) and (either parentSupervision is true or age is greater than or equal to 18).