From 2758e878703584fd361351e7727c1d1d1cdf84bf Mon Sep 17 00:00:00 2001 From: Jan Pearce Date: Tue, 26 Aug 2025 09:46:46 -0400 Subject: [PATCH 1/6] improve ternary code section --- source/ch4_conditionals.ptx | 44 +++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 04b1e6b..0290b45 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -385,17 +385,15 @@ The switch statement is not used very often, and we recommend you do not -
- Boolean Operators +
+ The Ternary Operator

Boolean operators simple comparisons compound Boolean expressions The conditionals used in the if statement can be Boolean variables, simple comparisons, and compound Boolean expressions.

ternary operator -Java also supports the boolean expression using the ternary operator -condition ? trueValue : falseValue. This operator tests a condition as part -of an assignment statement. The following table summarizes how this works: +Java also provides the ternary operator condition ? valueIfTrue : valueIfFalse, which lets you use a boolean 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:

@@ -437,27 +435,35 @@ of an assignment statement. The following table summarizes how this works:

-Using this operator can make code shorter and more readable in cases where a simple conditional assignment is needed. +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.

- + - class Main { - public static void main(String[] args) { - int a = 4; - int x = 2; - - // Using the ternary operator - a = (a % 2 == 0) ? a * a : 3 * x - 1; +public class Ternary { + public static void main(String[] args) { + int a = 4; + int x = 2; + int outp; + + // ternary: + outp = (a % 2 == 0) ? (a * a) : (3 * x - 1); + System.out.println("Result: " + outp); + + // Equivalent using if/else + if (a % 2 == 0) { + outp = a * a; + } else { + outp = 3 * x - 1; + } - System.out.println("Result: " + a); - } - } - + System.out.println("Result: " + outp); + } +}

- In this example we are using this ternary operator to assign a value to a based on whether a is even or odd. If a is even, it will be squared; if odd, it will be instead be calculated as 3 * x - 1. 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. + In this example we are using this ternary operator to assign a value to a based on whether a is even or odd. If a is even, it will be squared; if odd, it will be instead be calculated as 3 * x - 1. 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.

From 3fcd04d4d22e541780235e35cfb71c724f3f98a4 Mon Sep 17 00:00:00 2001 From: Jan Pearce Date: Tue, 26 Aug 2025 09:50:43 -0400 Subject: [PATCH 2/6] improve output --- source/ch4_conditionals.ptx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 0290b45..770f388 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -447,7 +447,7 @@ public class Ternary { // ternary: outp = (a % 2 == 0) ? (a * a) : (3 * x - 1); - System.out.println("Result: " + outp); + System.out.println("ternary result: " + outp); // Equivalent using if/else if (a % 2 == 0) { @@ -456,7 +456,7 @@ public class Ternary { outp = 3 * x - 1; } - System.out.println("Result: " + outp); + System.out.println("if/else result: " + outp); } } From e35c6b378d117f04105b6919eac78ceeebf43a8a Mon Sep 17 00:00:00 2001 From: Jan Pearce Date: Tue, 26 Aug 2025 10:04:26 -0400 Subject: [PATCH 3/6] improve switch section --- source/ch4_conditionals.ptx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 770f388..4b972df 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -232,6 +232,8 @@ Java also supports a switch statement that acts something like the eli

The switch statement is not used very often, and we recommend you do not use it. First, it is not as powerful as the else if model because the switch variable can only be compared for equality with an integer or enumerated constant. Second, it is very easy to forget to put in the break statement, so it is more error-prone. If the break statement is left out then then the next alternative will be automatically executed. For example, if the grade was 95 and the break was omitted from the case 9: alternative then the program would print(out both A and B.)

+

+ Finally, the switch statement does not support relational expressions such as greater than or less than. So you cannot use it to completely replace the elif. Even with the new features of Java 14+ the switch statement is still limited to constant comparisons using equality.

@@ -479,7 +481,7 @@ public class Ternary {
  • - Java's switch statement is similar to Python's match statement, but it only supports equality checks against constant values and does not evaluate relational expressions like greater than or less than. + Java's switch statement is similar to Python's match statement, but it only supports equality checks against constant values and does not evaluate relational expressions like greater than or less than.

  • From 72896c90e0ef02adf6fa4067d31ce05a23f08129 Mon Sep 17 00:00:00 2001 From: Jan Pearce Date: Tue, 26 Aug 2025 10:10:09 -0400 Subject: [PATCH 4/6] clarifications of match-case --- source/ch4_conditionals.ptx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 4b972df..186c522 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -162,12 +162,12 @@ public class ElseIf { Using the <c>switch</c> Statement

    -Java also supports a switch statement that acts something like the elif statement of Python under certain conditions. To write the grade program using a switch statement we would use the following: +Java also supports a switch statement that acts something like the elif or Python match statement of Python under certain conditions. To write the grade program using a switch statement we would use the following:

    - Depending on your knowledge and experience with Python you may be questioning why we are not using the match statement in our Python examples. The answer is that this book currently runs its active code examples using Python 3.7, which does not support the match statement which was introduced in Python 3.10. Below is an example of the match statement similar to our grade method. + The match - case statement was introduced in Python 3.10, so doesn't run in earlier version of Python. Here is an example using Python's match - case structure.

    Match Case Example @@ -192,7 +192,7 @@ Java also supports a switch statement that acts something like the eli

    switch - The switch statement in Java provides a clean and efficient alternative to chaining multiple if-else conditions, especially when comparing a single variable against several constant values. It supports a variety of data types, including primitive types (byte, short, char, int), their wrapper classes, enumerations, and String (introduced in Java 7). Each case within a switch must be defined using a constant expression, and duplicate case values are not permitted. By default, control flow "falls through" from one case to the next unless a break, return, or throw statement is used to terminate execution. + The switch statement in Java provides an alternative to chaining multiple if-else conditions, when comparing a single variable against several constant values. It supports a variety of data types, including primitive types (byte, short, char, int), their wrapper classes, enumerations, and String (introduced in Java 7). Each case within a switch must be defined using a constant expression, and duplicate case values are not permitted. By default, control flow "falls through" from one case to the next unless a break, return, or throw statement is used to terminate execution.

    From e6af616fd5c284c436ab4eb0874b46a3957acd15 Mon Sep 17 00:00:00 2001 From: Jan Pearce Date: Tue, 26 Aug 2025 10:20:01 -0400 Subject: [PATCH 5/6] add comments about integer division --- source/ch4_conditionals.ptx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 186c522..8238a56 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -173,6 +173,7 @@ Java also supports a switch statement that acts something like the eli Match Case Example grade = 85 + # Convert grade to a scale of 0-10 using integer division tempgrade = grade // 10 def grading(tempgrade): match grade: @@ -204,8 +205,9 @@ Java also supports a switch statement that acts something like the eli public class SwitchUp { - public static void main(String args[]) { + public static void main(String args[]) { int grade = 85; + // Convert grade to a scale of 0-10 using integer division int tempgrade = grade / 10; switch(tempgrade) { case 10: @@ -224,7 +226,7 @@ Java also supports a switch statement that acts something like the eli default: System.out.println('F'); } - } + } } From 982643d39ac626aaac00a19ef9bd1036cf43525a Mon Sep 17 00:00:00 2001 From: Jan Pearce Date: Tue, 26 Aug 2025 10:29:03 -0400 Subject: [PATCH 6/6] remove redundency --- source/ch4_conditionals.ptx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 8238a56..382e671 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -162,8 +162,8 @@ public class ElseIf { Using the <c>switch</c> Statement

    -Java also supports a switch statement that acts something like the elif or Python match statement of Python under certain conditions. To write the grade program using a switch statement we would use the following: -

    +Java also supports a switch statement that acts something like the elif or Python match statement under certain conditions. To write the grade program using a switch statement we would use the following: +