You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The conditionals used in the if statement can be <term>Boolean variables</term>, <term>simple comparisons</term>, and <term>compound Boolean expressions</term>.
393
393
</p>
394
394
395
395
<p><idx>ternary operator</idx>
396
-
Java also supports the <c>boolean</c> expression using the ternary operator
397
-
<c>condition ? trueValue : falseValue</c>. This operator tests a condition as part
398
-
of an assignment statement. The following table summarizes how this works:
396
+
Java also provides the ternary operator <c>condition ? valueIfTrue : valueIfFalse</c>, which lets you use a <c>boolean</c> test directly inside an assignment. If the condition is true, the first value is chosen; otherwise, the second value is used. The table below summarizes how it works:
399
397
</p>
400
398
401
399
<table>
@@ -437,27 +435,35 @@ of an assignment statement. The following table summarizes how this works:
437
435
</table>
438
436
439
437
<p>
440
-
Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed.
438
+
Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. See the following as an example where we see the same logic implemented in two different ways.
441
439
</p>
442
-
<programxml:id="java-ternary" interactive="activecode" language ="java">
440
+
<programxml:id="java-ternary"interactive="activecode" language ="java">
443
441
<code>
444
-
class Main {
445
-
public static void main(String[] args) {
446
-
int a = 4;
447
-
int x = 2;
448
-
449
-
// Using the ternary operator
450
-
a = (a % 2 == 0) ? a * a : 3 * x - 1;
442
+
public class Ternary {
443
+
public static void main(String[] args) {
444
+
int a = 4;
445
+
int x = 2;
446
+
int outp;
447
+
448
+
// ternary:
449
+
outp = (a % 2 == 0) ? (a * a) : (3 * x - 1);
450
+
System.out.println("Result: " + outp);
451
+
452
+
// Equivalent using if/else
453
+
if (a % 2 == 0) {
454
+
outp = a * a;
455
+
} else {
456
+
outp = 3 * x - 1;
457
+
}
451
458
452
-
System.out.println("Result: " + a);
453
-
}
454
-
}
455
-
459
+
System.out.println("Result: " + outp);
460
+
}
461
+
}
456
462
</code>
457
463
</program>
458
464
459
465
<p>
460
-
In this example we are using this ternary operator to assign a value to <c>a</c> based on whether <c>a</c> is even or odd. If <c>a</c> is even, it will be squared; if odd, it will be instead be calculated as <c>3 * x - 1</c>. This is a concise way to write conditional assignments in Java. However, it should be used reasonably, as it can make code less readable if overused or used in complex expressions.
466
+
In this example we are using this ternary operator to assign a value to <c>a</c> based on whether <c>a</c> is even or odd. If <c>a</c> is even, it will be squared; if odd, it will be instead be calculated as <c>3 * x - 1</c>. This is a concise way to write conditional assignments in Java. However, you might want to use it sparingly, as it can make code less readable if overused or used with complex expressions.
0 commit comments