|
4 | 4 | <chapter xml:id="conditionals"> |
5 | 5 | <title>Conditionals</title> |
6 | 6 |
|
7 | | - <section xml:id="simple-if"> |
8 | | - <title>Using the Simple <c>if</c> Statement</title> |
| 7 | + <section xml:id="using-conditional-in-java"> <title>Using Conditional Statements in Java</title> |
9 | 8 | <p><idx>conditional statements</idx> |
10 | 9 | Conditional statements in Python and Java are very similar. |
11 | 10 | In Python we have three patterns: |
@@ -50,9 +49,9 @@ if score >= 90: # Note the colon at the end of the line |
50 | 49 | Once again you can see that in Java the curly braces define a block rather than indentation. |
51 | 50 | In Java, the parentheses around the condition are required because it is technically a function that evaluates to <c>True</c> or <c>False</c>. |
52 | 51 | </p> |
53 | | - </section> |
| 52 | + |
54 | 53 |
|
55 | | - <section xml:id="use-the-if-else-statement"> |
| 54 | + <subsection xml:id="use-the-if-else-statement"> |
56 | 55 | <title>Using the <c>if</c> - <c>else</c> Statement</title> |
57 | 56 |
|
58 | 57 | <p><xref ref= "python-if-else" text="type-global"/> shows how the <c>if</c> - <c>else</c>statement is written in Python.</p> |
@@ -85,9 +84,9 @@ if score >= 90: # Note the colon at the end of the line |
85 | 84 | </code> |
86 | 85 | </program> |
87 | 86 | </listing> |
88 | | - </section> |
| 87 | + </subsection> |
89 | 88 |
|
90 | | - <section xml:id="elif"> |
| 89 | + <subsection xml:id="elif"> |
91 | 90 | <title>Can we use <c>elif</c>?</title> |
92 | 91 |
|
93 | 92 | <p><idx><c>elif</c> statement</idx> |
@@ -172,9 +171,9 @@ public class ElseIf { |
172 | 171 | </code> <tests> </tests> |
173 | 172 | </program> |
174 | 173 | </listing> |
175 | | - </section> |
| 174 | + </subsection> |
176 | 175 |
|
177 | | - <section xml:id="switch"> |
| 176 | + <subsection xml:id="switch"> |
178 | 177 | <title>Using the <c>switch</c> Statement</title> |
179 | 178 |
|
180 | 179 | <p> |
@@ -255,9 +254,94 @@ The <c>switch</c> statement is not used very often, and we recommend you do not |
255 | 254 | </p> |
256 | 255 | <p> |
257 | 256 | Finally, the <c>switch</c> statement does not support relational expressions such as greater than or less than. So you cannot use it to completely replace the <c>elif</c>. Even with the new features of Java 14+ the <c>switch</c> statement is still limited to constant comparisons using equality.</p> |
| 257 | + </subsection> |
258 | 258 | </section> |
259 | 259 |
|
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"> |
261 | 345 | <title>Exception Handling</title> |
262 | 346 |
|
263 | 347 | <p> |
@@ -411,92 +495,6 @@ The <c>switch</c> statement is not used very often, and we recommend you do not |
411 | 495 | 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. |
412 | 496 | </p> |
413 | 497 |
|
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 | | - |
500 | 498 | </section> |
501 | 499 | <section xml:id="chapter4_summary"> |
502 | 500 | <title>Summary & Reading Questions</title> |
|
0 commit comments