Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions source/ch4_conditionals.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,18 @@ public class ElseIf {
<title>Using the <c>switch</c> Statement</title>

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

<note>
<p>
Depending on your knowledge and experience with Python you may be questioning why we are not using the <c>match</c> 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 <c>match</c> statement which was introduced in Python 3.10. Below is an example of the <c>match</c> statement similar to our grade method.
The <c>match - case</c> statement was introduced in Python 3.10, so doesn't run in earlier version of Python. Here is an example using Python's <c>match - case</c> structure.
</p>
<program xml:id="python-match" language="python">
<title>Match Case Example</title>
<code>
grade = 85
# Convert grade to a scale of 0-10 using integer division
tempgrade = grade // 10
def grading(tempgrade):
match grade:
Expand All @@ -192,7 +193,7 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
</note>

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

<p>
Expand All @@ -204,8 +205,9 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
<program xml:id="java-switch-up" interactive="activecode" language="java">
<code>
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:
Expand All @@ -224,14 +226,16 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
default:
System.out.println('F');
}
}
}
}
</code> <tests> </tests>
</program>

<p>
The <c>switch</c> statement is not used very often, and we recommend you do not use it. First, it is not as powerful as the <c>else if</c> 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 <c>break</c> 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 <c>break</c> was omitted from the <c>case 9:</c> alternative then the program would print(out both A and B.)
</p>
<p>
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>
</section>

<section xml:id="exception-handling">
Expand Down Expand Up @@ -385,17 +389,15 @@ The <c>switch</c> statement is not used very often, and we recommend you do not

</section>

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

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

<p><idx>ternary operator</idx>
Java also supports the <c>boolean</c> expression using the ternary operator
<c>condition ? trueValue : falseValue</c>. This operator tests a condition as part
of an assignment statement. The following table summarizes how this works:
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:
</p>

<table>
Expand Down Expand Up @@ -437,27 +439,35 @@ of an assignment statement. The following table summarizes how this works:
</table>

<p>
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.
</p>
<program xml:id="java-ternary" interactive ="activecode" language ="java">
<program xml:id="java-ternary" interactive="activecode" language ="java">
<code>
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);
}
}
</code>
</program>

<p>
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.
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.
</p>


Expand All @@ -473,7 +483,7 @@ Using this operator can make code shorter and more readable in cases where a sim
</li>
<li>
<p>
Java's <c>switch</c> statement is similar to Python's <c>match</c> statement, but it only supports equality checks against constant values and does not evaluate relational expressions like greater than or less than.
Java's <c>switch</c> statement is similar to Python's <c>match</c> statement, but it only supports equality checks against constant values and does not evaluate relational expressions like greater than or less than.
</p>

</li>
Expand Down