logo

Villkorlig operatör i Java

I Java, villkorade operatörer kontrollera villkoret och bestämmer det önskade resultatet på grundval av båda villkoren. I det här avsnittet kommer vi att diskutera villkorlig operatör i Java.

Typer av villkorlig operatör

Det finns tre typer av villkorlig operatör i Java :

  • Villkorligt OCH
  • Villkorligt ELLER
  • Ternär operatör
Operatör Symbol
Villkorligt eller logiskt OCH &&
Villkorligt eller logiskt ELLER ||
Ternär operatör ?:

Villkorligt OCH

Operatorn tillämpas mellan två booleska uttryck. Det betecknas med de två AND-operatorerna (&&). Det returnerar sant om och endast om båda uttrycken är sanna, annars returnerar det falskt.

Uttryck 1 Uttryck 2 Uttryck1 && Uttryck2
Sann Falsk Falsk
Falsk Sann Falsk
Falsk Falsk Falsk
Sann Sann Sann

Villkorligt ELLER

Operatorn tillämpas mellan två booleska uttryck. Den betecknas med två ELLER-operatorn (||). Det returnerar sant om något av uttrycket är sant, annars returnerar det falskt.

Uttryck 1 Uttryck 2 Uttryck1 || Uttryck 2
Sann Sann Sann
Sann Falsk Sann
Falsk Sann Sann
Falsk Falsk Falsk

Låt oss skapa ett Java-program och använda den villkorliga operatorn.

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

Ternär operatör

Betydelsen av ternär består av tre delar. De ternär operatör (? :) består av tre operander. Det används för att utvärdera booleska uttryck. Operatören bestämmer vilket värde som ska tilldelas variabeln. Det är den enda villkorade operatören som accepterar tre operander. Den kan användas istället för if-else-satsen. Det gör koden mycket mer lättläst, läsbar och kortare.

Obs: Varje kod som använder en if-else-sats kan inte ersättas med en ternär operator.

Syntax:

 variable = (condition) ? expression1 : expression2 

Ovanstående uttalande säger att om tillståndet återkommer sant, uttryck1 blir avrättad, annars uttryck2 exekveras och det slutliga resultatet lagras i en variabel.

Villkorlig operatör i Java

Låt oss förstå den ternära operatorn genom flödesschemat.

Villkorlig operatör i 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); } } 

Produktion

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

Låt oss se ett annat exempel som utvärderar det största av tre tal med den ternära operatorn.

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

Produktion

 The largest number is: 89 

I programmet ovan har vi tagit tre variabler x, y och z med värdena 69, 89 respektive 79. Uttrycket (x > y) ? (x > z ? x : z) : (y > z ? y : z) utvärderar det största antalet av tre siffror och lagrar slutresultatet i variabeln störstaNumber. Låt oss förstå exekveringsordningen för uttrycket.

Villkorlig operatör i Java

Först kontrollerar den uttrycket (x > y) . Om det returnerar sant uttrycket (x > z ? x : z) exekveras, annars uttrycket (y > z ? y : z) blir avrättad.

När uttrycket (x > z ? x : z) exekveras, kontrollerar den ytterligare tillståndet x > z . Om villkoret returnerar sant returneras värdet av x, annars returneras värdet av z.

När uttrycket (y > z ? y : z) exekveras den kontrollerar villkoret ytterligare y > z . Om villkoret returnerar sant returneras värdet av y, annars returneras värdet av z.

Därför får vi det största av tre tal med den ternära operatorn.