Skip to content

Commit 2758e87

Browse files
committed
improve ternary code section
1 parent 0433911 commit 2758e87

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

source/ch4_conditionals.ptx

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,15 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
385385

386386
</section>
387387

388-
<section xml:id="boolean-operators">
389-
<title>Boolean Operators</title>
388+
<section xml:id="ternary-operator">
389+
<title>The Ternary Operator</title>
390390

391391
<p><idx>Boolean operators</idx> <idx>simple comparisons</idx> <idx>compound Boolean expressions</idx>
392392
The conditionals used in the if statement can be <term>Boolean variables</term>, <term>simple comparisons</term>, and <term>compound Boolean expressions</term>.
393393
</p>
394394

395395
<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:
399397
</p>
400398

401399
<table>
@@ -437,27 +435,35 @@ of an assignment statement. The following table summarizes how this works:
437435
</table>
438436

439437
<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.
441439
</p>
442-
<program xml:id="java-ternary" interactive ="activecode" language ="java">
440+
<program xml:id="java-ternary" interactive="activecode" language ="java">
443441
<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+
}
451458

452-
System.out.println("Result: " + a);
453-
}
454-
}
455-
459+
System.out.println("Result: " + outp);
460+
}
461+
}
456462
</code>
457463
</program>
458464

459465
<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.
461467
</p>
462468

463469

0 commit comments

Comments
 (0)