logo

מפעיל מותנה ב-Java

ב-Java, מפעילים מותנים בדוק את התנאי ומחליט על התוצאה הרצויה על בסיס שני התנאים. בחלק זה, נדון ב אופרטור מותנה ב-Java.

סוגי מפעיל מותנה

ישנם שלושה סוגים של המותנה מפעיל ב-Java :

  • AND מותנה
  • OR מותנה
  • מפעיל טרנרי
מַפעִיל סֵמֶל
AND מותנה או לוגי &&
OR מותנה או לוגי ||
מפעיל טרנרי ?:

AND מותנה

האופרטור מוחל בין שני ביטויים בוליאניים. זה מסומן על ידי שני אופרטורים AND (&&). היא מחזירה אמת אם ורק אם שני הביטויים נכונים, אחרת מחזירה שקר.

ביטוי 1 ביטוי 2 ביטוי1 && ביטוי2
נָכוֹן שֶׁקֶר שֶׁקֶר
שֶׁקֶר נָכוֹן שֶׁקֶר
שֶׁקֶר שֶׁקֶר שֶׁקֶר
נָכוֹן נָכוֹן נָכוֹן

OR מותנה

האופרטור מוחל בין שני ביטויים בוליאניים. זה מסומן על ידי אופרטור שני OR (||). זה מחזיר אמת אם כל אחד מהביטויים הוא אמת, אחרת מחזיר false.

ביטוי 1 ביטוי 2 ביטוי1 || ביטוי 2
נָכוֹן נָכוֹן נָכוֹן
נָכוֹן שֶׁקֶר נָכוֹן
שֶׁקֶר נָכוֹן נָכוֹן
שֶׁקֶר שֶׁקֶר שֶׁקֶר

בואו ניצור תוכנית Java ונשתמש באופרטור המותנה.

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

מפעיל טרנרי

המשמעות של מְשּוּלָשׁ מורכב משלושה חלקים. ה אופרטור שליש (? :) מורכב משלושה אופרנדים. הוא משמש להערכת ביטויים בוליאניים. האופרטור מחליט איזה ערך יוקצה למשתנה. זהו האופרטור המותנה היחיד שמקבל שלושה אופרנדים. ניתן להשתמש בו במקום הצהרת if-else. זה הופך את הקוד להרבה יותר קל, קריא וקצר יותר.

הערה: לא ניתן להחליף כל קוד המשתמש במשפט if-else באופרטור שליש.

תחביר:

 variable = (condition) ? expression1 : expression2 

ההצהרה לעיל קובעת שאם התנאי חוזר נכון, ביטוי1 מוצא להורג, אחרת ביטוי2 מבוצע והתוצאה הסופית מאוחסנת במשתנה.

מפעיל מותנה ב-Java

בואו נבין את האופרטור השלישוני דרך תרשים הזרימה.

מפעיל מותנה ב-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); } } 

תְפוּקָה

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

בואו נראה דוגמה נוספת שמעריכה את המספרים הגדול ביותר מבין שלושה באמצעות האופרטור השלישוני.

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

תְפוּקָה

 The largest number is: 89 

בתוכנית לעיל, לקחנו שלושה משתנים x, y ו-z בעלי הערכים 69, 89 ו-79, בהתאמה. הביטוי (x > y) ? (x > z ? x : z) : (y > z ? y : z) מעריך את המספר הגדול ביותר מבין שלושה מספרים ומאחסן את התוצאה הסופית במשתנה greatestNumber. בואו נבין את סדר הביצוע של הביטוי.

מפעיל מותנה ב-Java

ראשית, הוא בודק את הביטוי (x > y) . אם הוא מחזיר נכון הביטוי (x > z ? x : z) יוצא להורג, אחרת הביטוי (y > z ? y : z) יוצא להורג.

כאשר הביטוי (x > z ? x : z) מבוצע, הוא בודק עוד יותר את המצב x > z . אם התנאי מחזיר אמת הערך של x מוחזר, אחרת הערך של z מוחזר.

כאשר הביטוי (y > z ? y : z) מבוצע זה בודק עוד יותר את המצב y > z . אם התנאי מחזיר אמת הערך של y מוחזר, אחרת הערך של z מוחזר.

לכן, אנו מקבלים את המספרים הגדול ביותר מבין שלושה באמצעות האופרטור השלישוני.