Skip to content

Commit 443e2ae

Browse files
committed
moved ternary operator before file handling
1 parent 3c923ce commit 443e2ae

1 file changed

Lines changed: 85 additions & 87 deletions

File tree

source/ch4_conditionals.ptx

Lines changed: 85 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,91 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
257257
</subsection>
258258
</section>
259259

260-
<section xml:id="exception-handling">
260+
<section xml:id="ternary-operator">
261+
<title>The Ternary Operator</title>
262+
263+
<p><idx>Boolean operators</idx> <idx>simple comparisons</idx> <idx>compound Boolean expressions</idx>
264+
The conditionals used in the if statement can be <term>Boolean variables</term>, <term>simple comparisons</term>, and <term>compound Boolean expressions</term>.
265+
</p>
266+
267+
<p><idx>ternary operator</idx>
268+
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. <xref ref="ternary-operator-table" text="type-global"/> summarizes how it works.
269+
</p>
270+
271+
<table xml:id="ternary-operator-table">
272+
<title>Ternary Operator in Java</title>
273+
<tabular>
274+
<row>
275+
<cell><term>Component</term></cell>
276+
<cell><term>Description</term></cell>
277+
</row>
278+
<row>
279+
<cell><c>condition</c></cell>
280+
<cell>The <c>boolean</c> expression that is evaluated (e.g., <c>a % 2 == 0</c>).</cell>
281+
</row>
282+
<row>
283+
<cell><c>?</c></cell>
284+
<cell> This is the ternary operator that separates the condition from the <c>trueValue</c>.</cell>
285+
</row>
286+
<row>
287+
<cell><c>trueValue</c></cell>
288+
<cell>The value assigned if the condition is true (e.g., <c>a * a</c>).</cell>
289+
</row>
290+
<row>
291+
<cell><c>:</c></cell>
292+
<cell> This is the ternary operator that separates the <c>trueValue</c> from the <c>falseValue</c>.</cell>
293+
</row>
294+
<row>
295+
<cell><c>falseValue</c></cell>
296+
<cell>The value assigned if the condition is false (e.g., <c>3 * x - 1</c>).</cell>
297+
</row>
298+
<row>
299+
<cell>Example Usage</cell>
300+
<cell><c>a = a % 2 == 0 ? a * a : 3 * x - 1</c></cell>
301+
</row>
302+
<row>
303+
<cell>Equivalent if-else Code</cell>
304+
<cell>Can also be written with a regular <c>if-else</c> statement, but the ternary form is more concise.</cell>
305+
</row>
306+
</tabular>
307+
</table>
308+
309+
<p>
310+
Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. <xref ref="java-ternary" text="type-global"/> shows an example where we see the same logic implemented in two different ways.
311+
</p>
312+
<listing xml:id="java-ternary">
313+
<program interactive="activecode" language ="java">
314+
<code>
315+
public class Ternary {
316+
public static void main(String[] args) {
317+
int a = 4;
318+
int x = 2;
319+
int outp;
320+
321+
// ternary:
322+
outp = (a % 2 == 0) ? (a * a) : (3 * x - 1);
323+
System.out.println("ternary result: " + outp);
324+
325+
// Equivalent using if/else
326+
if (a % 2 == 0) {
327+
outp = a * a;
328+
} else {
329+
outp = 3 * x - 1;
330+
}
331+
332+
System.out.println("if/else result: " + outp);
333+
}
334+
}
335+
</code>
336+
</program>
337+
</listing>
338+
339+
<p>
340+
In <xref ref="java-ternary" text="type-global"/>, 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.
341+
</p>
342+
</section>
343+
344+
<section xml:id="exception-handling">
261345
<title>Exception Handling</title>
262346

263347
<p>
@@ -411,92 +495,6 @@ The <c>switch</c> statement is not used very often, and we recommend you do not
411495
Note that as with other structures in Java, <c>try-catch</c> blocks blocks must be encased with braces <c>{}</c>. The most important part of this code is, after <c>catch</c>, there is a set of parenthesis with an exception type and a variable name <c>catch (InputMismatchException e)</c>. This is where we declare a <c>InputMismatchException</c> exception and name it with the variable name <c>e</c>. It is common practice, though not a requirement, to name exception variables <c>e</c> in this manner.
412496
</p>
413497

414-
</section>
415-
416-
<section xml:id="ternary-operator">
417-
<title>The Ternary Operator</title>
418-
419-
<p><idx>Boolean operators</idx> <idx>simple comparisons</idx> <idx>compound Boolean expressions</idx>
420-
The conditionals used in the if statement can be <term>Boolean variables</term>, <term>simple comparisons</term>, and <term>compound Boolean expressions</term>.
421-
</p>
422-
423-
<p><idx>ternary operator</idx>
424-
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. <xref ref="ternary-operator-table" text="type-global"/> summarizes how it works.
425-
</p>
426-
427-
<table xml:id="ternary-operator-table">
428-
<title>Ternary Operator in Java</title>
429-
<tabular>
430-
<row>
431-
<cell><term>Component</term></cell>
432-
<cell><term>Description</term></cell>
433-
</row>
434-
<row>
435-
<cell><c>condition</c></cell>
436-
<cell>The <c>boolean</c> expression that is evaluated (e.g., <c>a % 2 == 0</c>).</cell>
437-
</row>
438-
<row>
439-
<cell><c>?</c></cell>
440-
<cell> This is the ternary operator that separates the condition from the <c>trueValue</c>.</cell>
441-
</row>
442-
<row>
443-
<cell><c>trueValue</c></cell>
444-
<cell>The value assigned if the condition is true (e.g., <c>a * a</c>).</cell>
445-
</row>
446-
<row>
447-
<cell><c>:</c></cell>
448-
<cell> This is the ternary operator that separates the <c>trueValue</c> from the <c>falseValue</c>.</cell>
449-
</row>
450-
<row>
451-
<cell><c>falseValue</c></cell>
452-
<cell>The value assigned if the condition is false (e.g., <c>3 * x - 1</c>).</cell>
453-
</row>
454-
<row>
455-
<cell>Example Usage</cell>
456-
<cell><c>a = a % 2 == 0 ? a * a : 3 * x - 1</c></cell>
457-
</row>
458-
<row>
459-
<cell>Equivalent if-else Code</cell>
460-
<cell>Can also be written with a regular <c>if-else</c> statement, but the ternary form is more concise.</cell>
461-
</row>
462-
</tabular>
463-
</table>
464-
465-
<p>
466-
Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. <xref ref="java-ternary" text="type-global"/> shows an example where we see the same logic implemented in two different ways.
467-
</p>
468-
<listing xml:id="java-ternary">
469-
<program interactive="activecode" language ="java">
470-
<code>
471-
public class Ternary {
472-
public static void main(String[] args) {
473-
int a = 4;
474-
int x = 2;
475-
int outp;
476-
477-
// ternary:
478-
outp = (a % 2 == 0) ? (a * a) : (3 * x - 1);
479-
System.out.println("ternary result: " + outp);
480-
481-
// Equivalent using if/else
482-
if (a % 2 == 0) {
483-
outp = a * a;
484-
} else {
485-
outp = 3 * x - 1;
486-
}
487-
488-
System.out.println("if/else result: " + outp);
489-
}
490-
}
491-
</code>
492-
</program>
493-
</listing>
494-
495-
<p>
496-
In <xref ref="java-ternary" text="type-global"/>, 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.
497-
</p>
498-
499-
500498
</section>
501499
<section xml:id="chapter4_summary">
502500
<title>Summary &amp; Reading Questions</title>

0 commit comments

Comments
 (0)