Skip to content

Commit e35c6b3

Browse files
committed
improve switch section
1 parent 3fcd04d commit e35c6b3

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

source/ch4_conditionals.ptx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
232232
<p>
233233
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.)
234234
</p>
235+
<p>
236+
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>
235237
</section>
236238

237239
<section xml:id="exception-handling">
@@ -479,7 +481,7 @@ public class Ternary {
479481
</li>
480482
<li>
481483
<p>
482-
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.
484+
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.
483485
</p>
484486

485487
</li>

0 commit comments

Comments
 (0)