Les opérateurs relationnels
| Symbole | Exemple | Cas où le résultat renvoit True |
|---|
| > | opérande1 > opérande2 | opérande1 est strictement supérieur à opérande2 |
| >= | opérande1 >= opérande2 | opérande1 est supérieur ou égal à opérande2 |
| < | opérande1 < opérande2 | opérande1 est strictement inférieur à opérande2 |
| <= | opérande1 <= opérande2 | opérande1 est inférieur ou égal à opérande2 |
| == | opérande1 == opérande2 | opérande1 est égal à opérande2 |
| != | opérande1 != opérande2 | opérande1 n'est pas égal à opérande2 |
Exemple
public class OperateursRelationnels {
public static void main
(String[] args
) {
//a few numbers
int i = 37;
int j = 42;
int k = 42;
System.
out.
println("Variable values..."); System.
out.
println(" i = " + i
); System.
out.
println(" j = " + j
); System.
out.
println(" k = " + k
);
//greater than
System.
out.
println("Greater than..."); System.
out.
println(" i > j = " +
(i
> j
)); //false System.
out.
println(" j > i = " +
(j
> i
)); //true System.
out.
println(" k > j = " +
(k
> j
)); //false, they are equal
//greater than or equal to
System.
out.
println("Greater than or equal to..."); System.
out.
println(" i >= j = " +
(i
>= j
)); //false System.
out.
println(" j >= i = " +
(j
>= i
)); //true System.
out.
println(" k >= j = " +
(k
>= j
)); //true
//less than
System.
out.
println("Less than..."); System.
out.
println(" i < j = " +
(i
< j
)); //true System.
out.
println(" j < i = " +
(j
< i
)); //false System.
out.
println(" k < j = " +
(k
< j
)); //false
//less than or equal to
System.
out.
println("Less than or equal to..."); System.
out.
println(" i <= j = " +
(i
<= j
)); //true System.
out.
println(" j <= i = " +
(j
<= i
)); //false System.
out.
println(" k <= j = " +
(k
<= j
)); //true
//equal to
System.
out.
println("Equal to..."); System.
out.
println(" i == j = " +
(i == j
)); //false System.
out.
println(" k == j = " +
(k == j
)); //true
//not equal to
System.
out.
println("Not equal to..."); System.
out.
println(" i != j = " +
(i
!= j
)); //true System.
out.
println(" k != j = " +
(k
!= j
)); //false
}
}
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
Réseaux sociaux
Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher les interactions avec les réseaux sociaux sur ces pages.