logo

Javaの条件演算子

Javaでは、 条件演算子 条件を確認し、両方の条件に基づいて望ましい結果を決定します。このセクションでは、 Java の条件演算子。

条件演算子の種類

条件式には3種類あります Javaの演算子 :

  • 条件付きAND
  • 条件付きOR
  • 三項演算子
オペレーター シンボル
条件付きまたは論理 AND &&
条件付きまたは論理和 ||
三項演算子 ?:

条件付きAND

演算子は 2 つのブール式の間に適用されます。これは 2 つの AND 演算子 (&&) で表されます。両方の式が true の場合にのみ true を返し、それ以外の場合は false を返します。

式1 式2 式 1 && 式 2
真実 間違い 間違い
間違い 真実 間違い
間違い 間違い 間違い
真実 真実 真実

条件付きOR

演算子は 2 つのブール式の間に適用されます。これは 2 つの OR 演算子 (||) で表されます。いずれかの式が true の場合は true を返し、それ以外の場合は false を返します。

式1 式2 式 1 ||式2
真実 真実 真実
真実 間違い 真実
間違い 真実 真実
間違い 間違い 間違い

Java プログラムを作成し、条件演算子を使用してみましょう。

ハッシュテーブル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);>

三項演算子

の意味 三項 3つの部分で構成されています。の 三項演算子 (? :) 3 つのオペランドで構成されます。ブール式を評価するために使用されます。演算子は変数にどの値を割り当てるかを決定します。これは、3 つのオペランドを受け入れる唯一の条件演算子です。 if-else ステートメントの代わりに使用できます。これにより、コードがはるかに簡単で読みやすく、短くなります。

Javaのchar + int

注: if-else ステートメントを使用するすべてのコードは、三項演算子で置き換えることはできません。

構文:

 variable = (condition) ? expression1 : expression2 

上記のステートメントは、条件が返された場合に次のように述べています。 true、式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 

三項演算子を使用して 3 つの数値の最大値を評価する別の例を見てみましょう。

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 

上記のプログラムでは、それぞれ値 69、89、および 79 を持つ 3 つの変数 x、y、および z を取得しました。表現 (x > y) ? (x > z ? x : z) : (y > z ? y : z) 3 つの数値の中で最大の数値を評価し、最終結果を変数 biggestNumber に格納します。式の実行順序を理解しましょう。

Javaの条件演算子

まず、式をチェックします (x > y) 。 true を返す場合、式は (x > z ? x : z) 実行されます。それ以外の場合は式が実行されます (y > z ? y : z) 処刑される。

という表現があったとき、 (x > z ? x : z) 実行されると、さらに条件がチェックされます x > z 。条件が true を返す場合は x の値が返され、それ以外の場合は z の値が返されます。

PowerShell 以上

という表現があったとき、 (y > z ? y : z) 実行され、条件がさらにチェックされます y > z 。条件が true を返す場合は y の値が返され、それ以外の場合は z の値が返されます。

したがって、三項演算子を使用して 3 つの数値のうち最大のものを取得します。