You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch4_conditionals.ptx
+11-7Lines changed: 11 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -155,9 +155,9 @@ We can get even closer to the <c>elif</c> statement by taking advantage of the J
155
155
<listingxml:id="java-if-else-no-nesting" >
156
156
<programinteractive="activecode"language="java">
157
157
<code>
158
-
public class ElseIf {
158
+
public class ElseIf {
159
159
public static void main(String args[]) {
160
-
int grade = 85;
160
+
int grade = 85;
161
161
if (grade < 60) {
162
162
System.out.println('F');
163
163
} else if (grade < 70) {
@@ -178,14 +178,15 @@ public class ElseIf {
178
178
<title>Using the <c>switch</c> Statement</title>
179
179
180
180
<p>
181
-
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:
181
+
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.<xrefref="python-match"text="type-global"/> shows how a grade program using a <c>switch</c> statement would look in Python.
182
182
</p>
183
183
184
184
<note>
185
185
<p>
186
186
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.
@@ -206,6 +207,7 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
206
207
print(grading(tempgrade))
207
208
</code>
208
209
</program>
210
+
</listing>
209
211
210
212
<p><idx><c>switch</c></idx>
211
213
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.
@@ -214,10 +216,11 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
214
216
<p>
215
217
<idx><c>switch</c> expressions</idx>
216
218
<idx><c>yield</c></idx>
217
-
Java 14 introduced <c>switch</c> <term>expressions</term>, enhancing functionality by allowing the <c>switch</c> to return values and eliminating <c>fall-through</c> via the <c>-></c> arrow syntax. These expressions can even use <c>yield</c> within code blocks for more complex evaluations. <term><c>yield</c></term> is used inside a switch expression’s block to produce the value of that expression, unlike <c>break</c> which simply exits a switch statement or loop. It’s important to note that traditional <c>switch</c> statements do not support <c>null</c> values and will throw a <c>NullPointerException</c> if evaluated with <c>null</c>. As the language evolves, newer versions of Java continue to extend <c>switch</c> capabilities with features like <c>pattern matching</c> and enhanced <c>type handling</c>, making it a more powerful and expressive tool for decision-making in Java programs.
219
+
Java 14 introduced <c>switch</c> <term>expressions</term>, enhancing functionality by allowing the <c>switch</c> to return values and eliminating <c>fall-through</c> via the <c>-></c> arrow syntax. These expressions can even use <c>yield</c> within code blocks for more complex evaluations. <term><c>yield</c></term> is used inside a switch expression’s block to produce the value of that expression, unlike <c>break</c> which simply exits a switch statement or loop. It’s important to note that traditional <c>switch</c> statements do not support <c>null</c> values and will throw a <c>NullPointerException</c> if evaluated with <c>null</c>. As the language evolves, newer versions of Java continue to extend <c>switch</c> capabilities with features like <c>pattern matching</c> and enhanced <c>type handling</c>, making it a more powerful and expressive tool for decision-making in Java programs. <xref ref="java-switch-up" text="type-global"/> shows how the <c>switch</c> expression is written in Java.
@@ -245,6 +248,7 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
245
248
}
246
249
</code> <tests> </tests>
247
250
</program>
251
+
</listing>
248
252
249
253
<p>
250
254
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.)
0 commit comments