diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 04b1e6b..382e671 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -162,17 +162,18 @@ 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 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 grade = 85 + # Convert grade to a scale of 0-10 using integer division tempgrade = grade // 10 def grading(tempgrade): match grade: @@ -192,7 +193,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.

@@ -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'); } - } + } } @@ -232,6 +234,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.

@@ -385,17 +389,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 +439,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("ternary 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("if/else 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.

@@ -473,7 +483,7 @@ Using this operator can make code shorter and more readable in cases where a sim
  • - 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.