CodeGym
CodeGym University
Apprentissage
Programme
Missions
Études et quiz
Jeux
Aide
Calendrier motivateur
Communauté
Utilisateurs
Forum
Discussion
Articles
Témoignages
Activité
Avis
Abonnements
Thème clair
Démarrer la formation maintenant
  • Toutes les questions
BenOitB
Niveau 8
Lyon
  • 09.06.2021
  • 334vues
  • 2commentaires

[Spoiler answer alert] Can someone explain to me the >= operator in this case ?

Question sur la mission Tri de trois nombres
Syntaxe Java,  Niveau 4,  Leçon 6
Résolues
0
Commentaires (2)
  • Populaires
  • Nouveau
  • Anciennes
Tu dois être connecté(e) pour laisser un commentaire
BenOitB
niveau Lyon, Lyon, France
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
niveau Tampa, Tampa, United States
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:
if (a > b && b > c) {
   //output1
} else if (a > c && c > b) {
   //output2
} else if (b > a && a > c) {
   //output3
}  else if (b > c && c > a) {
   //output4
} else if (c > a && a > b) {
   //output5
} else if (c > b && b > a) {
   //output6
}
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
Apprendre
  • Inscription
  • Cours de Java
  • Aide avec les missions
  • Tarification
  • Projets de jeu
  • Syntaxe Java
Communauté
  • Utilisateurs
  • Articles
  • Forum
  • Discussion
  • Témoignages
  • Activité
  • Affiliate Program
Société
  • À propos
  • Contacts
  • Avis
  • Salle de presse
  • CodeGym pour l'éducation
  • FAQ
  • Support
CodeGym CodeGym est un cours en ligne pour apprendre la programmation Java à partir de rien. Ce cours est le moyen idéal de maîtriser Java pour les débutants. Il contient plus de 1 200 missions avec vérification instantanée et l'essentiel de la théorie sur les fondamentaux de Java. Pour t'aider à réussir dans ton apprentissage, nous avons préparé tout un tas de fonctionnalités motivantes : questionnaires, projets de codage et contenu pour t'aider à apprendre efficacement et te lancer dans une carrière de développeur Java.
Suis-nous
Langue de l'interface
On ne naît pas programmeur, on le devient © 2023 CodeGym
MastercardVisa
On ne naît pas programmeur, on le devient © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.