[Spoiler answer alert] Can someone explain to me the >= operator in this case ?
Résolues
Commentaires (2)
- Populaires
- Nouveau
- Anciennes
Tu dois être connecté(e) pour laisser un commentaire
BenOitB
10 juin 2021, 13:47
Hi Guadalupe !
Ok, it makes perfectly sense and it goes back to what I was thinking. Great !
+1
Guadalupe Gagnon
9 juin 2021, 17:29utile
When you compare two numbers (number1 compared to number2) there are 3 possible outcomes:
1) the first number is greater than the second number
2) the first number is less than the second number
3) the first number is equal to the second number
Exampes:
1 > 1 // this is false as 1 is not greater than 1
2 > 1 // this is true as 2 is greater than 1
1 > 2 // this is false as 1 is not greater than 2
1 < 2 // this is true as 1 is less than 2
2 < 1 // this is false as 2 is not less than 1
2 < 2 // this is false as 2 is not less than 2
1 == 2 // this is false as 1 does not equal 2
2 == 1 // this is false as 2 does not equal 1
2 == 2 // this is true as 2 does equal 2
Java has two more operators, the greater than OR equal to operator (>=) and the less than OR equal to operator (<=) that allows you to check for two of those conditions at once.
As I mentioned in another answer to one of your posts, "if-else if" statements will only process until it reaches to first if condition that returns true. By only using an operator that checks two of those outcomes (ignoring the third) you allow bugs into the code and the code will not arrive at the correct solution. Example, lets say you removed the equality check:
Any combination of three numbers where any two are duplicated:
1, 2, 2
2, 1, 2
2, 2, 1
2, 2, 2
None of those compound if statements would be true because equality is not checked, and no output would be reached. +1