logo

Sąlyginis operatorius Java

Java, sąlyginiai operatoriai patikrinkite būklę ir pagal abi sąlygas nusprendžia norimą rezultatą. Šiame skyriuje aptarsime sąlyginis operatorius Java.

Sąlyginių operatorių tipai

Yra trys sąlyginės sąlygos operatorius Java :

  • Sąlyginis IR
  • Sąlyginis ARBA
  • Trečias operatorius
operatorius Simbolis
Sąlyginis arba loginis IR &&
Sąlyginis arba loginis ARBA ||
Trečias operatorius ?:

Sąlyginis IR

Operatorius taikomas tarp dviejų Būlio išraiškų. Jis žymimas dviem IR operatoriais (&&). Jis grąžina teisingą tada ir tik tada, kai abi išraiškos yra teisingos, kitu atveju grąžina false.

Išraiška1 Išraiška2 Išraiška1 && Išraiška2
Tiesa Netiesa Netiesa
Netiesa Tiesa Netiesa
Netiesa Netiesa Netiesa
Tiesa Tiesa Tiesa

Sąlyginis ARBA

Operatorius taikomas tarp dviejų Būlio išraiškų. Jis žymimas dviem OR operatoriais (||). Grąžinama tiesa, jei kuri nors išraiška yra teisinga, kitu atveju grąžinama false.

Išraiška1 Išraiška2 Išraiška1 || Išraiška2
Tiesa Tiesa Tiesa
Tiesa Netiesa Tiesa
Netiesa Tiesa Tiesa
Netiesa Netiesa Netiesa

Sukurkime Java programą ir naudokime sąlyginį operatorių.

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);>

Trečias operatorius

Prasmė trejetas susideda iš trijų dalių. The trinarinis operatorius (? :) susideda iš trijų operandų. Jis naudojamas Būlio išraiškoms įvertinti. Operatorius nusprendžia, kuri reikšmė bus priskirta kintamajam. Tai vienintelis sąlyginis operatorius, kuris priima tris operandus. Jis gali būti naudojamas vietoj if-else teiginio. Tai daro kodą daug lengvesnį, skaitomesnį ir trumpesnį.

Pastaba: kiekvieno kodo, kuriame naudojamas if-else teiginys, negalima pakeisti trijų dalių operatoriumi.

Sintaksė:

 variable = (condition) ? expression1 : expression2 

Aukščiau pateiktame teiginyje teigiama, kad jei sąlyga grįžta tiesa, išraiška1 bus įvykdyta mirties bausmė, kitaip išraiška2 vykdomas, o galutinis rezultatas išsaugomas kintamajame.

Sąlyginis operatorius Java

Supraskime trijų dalių operatorių per schemą.

Sąlyginis operatorius 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); } } 

Išvestis

 Value of y is: 90 Value of y is: 61 

Pažiūrėkime dar vieną pavyzdį, kuris įvertina didžiausią iš trijų skaičių, naudojant trijų dalių operatorių.

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); } } 

Išvestis

 The largest number is: 89 

Aukščiau pateiktoje programoje mes paėmėme tris kintamuosius x, y ir z, kurių reikšmės yra atitinkamai 69, 89 ir 79. Išsireiškimas (x > y) ? (x > z ? x : z) : (y > z ? y : z) įvertina didžiausią skaičių tarp trijų skaičių ir išsaugo galutinį rezultatą kintamajame didžiausias skaičius. Supraskime išraiškos vykdymo tvarką.

Sąlyginis operatorius Java

Pirma, jis patikrina išraišką (x > y) . Jei grąžinama tiesa, išraiška (x > z ? x : z) bus įvykdytas, kitaip išraiška (y > z ? y : z) įvykdoma mirties bausmė.

Kai išraiška (x > z ? x : z) įvykdoma, ji toliau tikrina būklę x > z . Jei sąlyga grąžina teisingą, grąžinama x reikšmė, kitu atveju grąžinama z reikšmė.

Kai išraiška (y > z ? y : z) įvykdomas ir toliau tikrinama būsena y > z . Jei sąlyga grąžina teisingą, grąžinama y reikšmė, kitu atveju grąžinama z reikšmė.

Todėl mes gauname didžiausią iš trijų skaičių, naudodami trijų dalių operatorių.