Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.


+16 votes

Are there any ways shorter to write the condition of if/else statement which compares 3 values than "If ( value1 == value2 && value2 == value3) {... " ?

Practice it counts "if (value1 == value2 == value3) {..." as error

asked in CSC211_Winter2018 by (8 points)

2 Answers

+12 votes

No because == is a relational operator that can only compare two operands. The code that you tried would compare the first two and find it to be true or false and then compare true/false to value3 which results in the error.

answered by (8 points)
+10 votes

I don't think there is another way of comparing 3 values other than using && or || statements, in this example.

answered by (8 points)