Les opérateurs relationnels

SymboleExempleCas où le résultat renvoit True
>opérande1 > opérande2opérande1 est strictement supérieur à opérande2
>=opérande1 >= opérande2opérande1 est supérieur ou égal à  opérande2
<opérande1 < opérande2opérande1 est strictement inférieur à opérande2
<=opérande1 <= opérande2opérande1 est inférieur ou égal à opérande2
==opérande1 == opérande2opérande1 est égal à opérande2
!=opérande1 != opérande2opérande1 n'est pas égal à opérande2

Exemple

  1. public class OperateursRelationnels {
  2. public static void main(String[] args) {
  3.  
  4. //a few numbers
  5. int i = 37;
  6. int j = 42;
  7. int k = 42;
  8. System.out.println("Variable values...");
  9. System.out.println(" i = " + i);
  10. System.out.println(" j = " + j);
  11. System.out.println(" k = " + k);
  12.  
  13. //greater than
  14. System.out.println("Greater than...");
  15. System.out.println(" i > j = " + (i > j)); //false
  16. System.out.println(" j > i = " + (j > i)); //true
  17. System.out.println(" k > j = " + (k > j)); //false, they are equal
  18.  
  19. //greater than or equal to
  20. System.out.println("Greater than or equal to...");
  21. System.out.println(" i >= j = " + (i >= j)); //false
  22. System.out.println(" j >= i = " + (j >= i)); //true
  23. System.out.println(" k >= j = " + (k >= j)); //true
  24.  
  25. //less than
  26. System.out.println("Less than...");
  27. System.out.println(" i < j = " + (i < j)); //true
  28. System.out.println(" j < i = " + (j < i)); //false
  29. System.out.println(" k < j = " + (k < j)); //false
  30.  
  31. //less than or equal to
  32. System.out.println("Less than or equal to...");
  33. System.out.println(" i <= j = " + (i <= j)); //true
  34. System.out.println(" j <= i = " + (j <= i)); //false
  35. System.out.println(" k <= j = " + (k <= j)); //true
  36.  
  37. //equal to
  38. System.out.println("Equal to...");
  39. System.out.println(" i == j = " + (i == j)); //false
  40. System.out.println(" k == j = " + (k == j)); //true
  41.  
  42. //not equal to
  43. System.out.println("Not equal to...");
  44. System.out.println(" i != j = " + (i != j)); //true
  45. System.out.println(" k != j = " + (k != j)); //false
  46.  
  47. }
  48. }

Ce qui donne en sortie :

Variable values...
 i = 37
 j = 42
 k = 42
Greater than...
 i > j = false
 j > i = true
 k > j = false
Greater than or equal to...
 i >= j = false
 j >= i = true
 k >= j = true
Less than...
 i < j = true
 j < i = false
 k < j = false
Less than or equal to...
 i <= j = true
 j <= i = false
 k <= j = true
Equal to...
 i == j = false
 k == j = true
Not equal to...
 i != j = true
 k != j = false

Document créé le 24/04/2005, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/programmation-operateur-relationnel.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.