logo

Opérateur conditionnel en Java

En Java, opérateurs conditionnels vérifie la condition et décide du résultat souhaité sur la base des deux conditions. Dans cette section, nous aborderons les opérateur conditionnel en Java.

Types d'opérateur conditionnel

Il existe trois types de conditionnel opérateur en Java :

comment vérifier les numéros bloqués sur Android
  • ET conditionnel
  • OU conditionnel
  • Opérateur ternaire
Opérateur Symbole
ET conditionnel ou logique &&
OU conditionnel ou logique ||
Opérateur ternaire ?:

ET conditionnel

L'opérateur s'applique entre deux expressions booléennes. Il est désigné par les deux opérateurs AND (&&). Il renvoie vrai si et seulement si les deux expressions sont vraies, sinon renvoie faux.

Expression1 Expression2 Expression1 && Expression2
Vrai FAUX FAUX
FAUX Vrai FAUX
FAUX FAUX FAUX
Vrai Vrai Vrai

OU conditionnel

L'opérateur s'applique entre deux expressions booléennes. Il est désigné par les deux opérateurs OR (||). Il renvoie vrai si l'une des expressions est vraie, sinon renvoie faux.

Expression1 Expression2 Expression1 || Expression2
Vrai Vrai Vrai
Vrai FAUX Vrai
FAUX Vrai Vrai
FAUX FAUX FAUX

Créons un programme Java et utilisons l'opérateur conditionnel.

ConditionalOperatorExample.java

 public class ConditionalOperatorExample { public static void main(String args[]) y<z); system.out.println((xz) && x<y); } < pre> <p> <strong>Output</strong> </p> <pre> true false </pre> <h3>Ternary Operator</h3> <p>The meaning of <strong>ternary</strong> is composed of three parts. The <strong>ternary operator (? :)</strong> consists of three operands. It is used to evaluate Boolean expressions. The operator decides which value will be assigned to the variable. It is the only conditional operator that accepts three operands. It can be used instead of the if-else statement. It makes the code much more easy, readable, and shorter.</p> <h4>Note: Every code using an if-else statement cannot be replaced with a ternary operator.</h4> <p> <strong>Syntax:</strong> </p> <pre> variable = (condition) ? expression1 : expression2 </pre> <p>The above statement states that if the condition returns <strong>true, expression1</strong> gets executed, else the <strong>expression2</strong> gets executed and the final result stored in a variable.</p> <img src="//techcodeview.com/img/java-tutorial/89/conditional-operator-java.webp" alt="Conditional Operator in Java"> <p>Let&apos;s understand the ternary operator through the flowchart.</p> <img src="//techcodeview.com/img/java-tutorial/89/conditional-operator-java-2.webp" alt="Conditional Operator in Java"> <p> <strong>TernaryOperatorExample.java</strong> </p> <pre> public class TernaryOperatorExample { public static void main(String args[]) { int x, y; x = 20; y = (x == 1) ? 61: 90; System.out.println(&apos;Value of y is: &apos; + y); y = (x == 20) ? 61: 90; System.out.println(&apos;Value of y is: &apos; + y); } } </pre> <p> <strong>Output</strong> </p> <pre> Value of y is: 90 Value of y is: 61 </pre> <p>Let&apos;s see another example that evaluates the largest of three numbers using the ternary operator.</p> <p> <strong>LargestNumberExample.java</strong> </p> <pre> public class LargestNumberExample { public static void main(String args[]) { int x=69; int y=89; int z=79; int largestNumber= (x &gt; y) ? (x &gt; z ? x : z) : (y &gt; z ? y : z); System.out.println(&apos;The largest numbers is: &apos;+largestNumber); } } </pre> <p> <strong>Output</strong> </p> <pre> The largest number is: 89 </pre> <p>In the above program, we have taken three variables x, y, and z having the values 69, 89, and 79, respectively. The expression <strong>(x &gt; y) ? (x &gt; z ? x : z) : (y &gt; z ? y : z)</strong> evaluates the largest number among three numbers and store the final result in the variable largestNumber. Let&apos;s understand the execution order of the expression.</p> <img src="//techcodeview.com/img/java-tutorial/89/conditional-operator-java-3.webp" alt="Conditional Operator in Java"> <p>First, it checks the expression <strong>(x &gt; y)</strong> . If it returns true the expression <strong>(x &gt; z ? x : z)</strong> gets executed, else the expression <strong>(y &gt; z ? y : z)</strong> gets executed.</p> <p>When the expression <strong>(x &gt; z ? x : z)</strong> gets executed, it further checks the condition <strong>x &gt; z</strong> . If the condition returns true the value of x is returned, else the value of z is returned.</p> <p>When the expression <strong>(y &gt; z ? y : z)</strong> gets executed it further checks the condition <strong>y &gt; z</strong> . If the condition returns true the value of y is returned, else the value of z is returned.</p> <p>Therefore, we get the largest of three numbers using the ternary operator.</p> <hr></z);>

Opérateur ternaire

Le sens de ternaire est composé de trois parties. Le opérateur ternaire (? :) se compose de trois opérandes. Il est utilisé pour évaluer des expressions booléennes. L'opérateur décide quelle valeur sera attribuée à la variable. C'est le seul opérateur conditionnel qui accepte trois opérandes. Il peut être utilisé à la place de l'instruction if-else. Cela rend le code beaucoup plus simple, lisible et plus court.

Remarque : tout code utilisant une instruction if-else ne peut pas être remplacé par un opérateur ternaire.

Syntaxe:

 variable = (condition) ? expression1 : expression2 

La déclaration ci-dessus indique que si la condition renvoie vrai, expression1 est exécuté, sinon le expression2 est exécuté et le résultat final est stocké dans une variable.

Opérateur conditionnel en Java

Comprenons l'opérateur ternaire à travers l'organigramme.

Opérateur conditionnel en Java

TernaryOperatorExample.java

 public class TernaryOperatorExample { public static void main(String args[]) { int x, y; x = 20; y = (x == 1) ? 61: 90; System.out.println(&apos;Value of y is: &apos; + y); y = (x == 20) ? 61: 90; System.out.println(&apos;Value of y is: &apos; + y); } } 

Sortir

activer java
 Value of y is: 90 Value of y is: 61 

Voyons un autre exemple qui évalue le plus grand des trois nombres à l'aide de l'opérateur ternaire.

LargestNumberExample.java

 public class LargestNumberExample { public static void main(String args[]) { int x=69; int y=89; int z=79; int largestNumber= (x &gt; y) ? (x &gt; z ? x : z) : (y &gt; z ? y : z); System.out.println(&apos;The largest numbers is: &apos;+largestNumber); } } 

Sortir

 The largest number is: 89 

Dans le programme ci-dessus, nous avons pris trois variables x, y et z ayant respectivement les valeurs 69, 89 et 79. L'expression (x > y) ? (x > z ? x : z) : (y > z ? y : z) évalue le plus grand nombre parmi trois nombres et stocke le résultat final dans la variable plus grandNombre. Comprenons l'ordre d'exécution de l'expression.

Opérateur conditionnel en Java

Tout d'abord, il vérifie l'expression (x > y) . Si cela renvoie vrai, l'expression (x > z ? x : z) est exécuté, sinon l'expression (y > z ? y : z) est exécuté.

Quand l'expression (x > z ? x : z) est exécuté, il vérifie en outre la condition x > z . Si la condition renvoie vrai, la valeur de x est renvoyée, sinon la valeur de z est renvoyée.

Quand l'expression (y > z ? y : z) est exécuté, il vérifie en outre la condition y > z . Si la condition renvoie vrai, la valeur de y est renvoyée, sinon la valeur de z est renvoyée.

Par conséquent, nous obtenons le plus grand des trois nombres en utilisant l’opérateur ternaire.