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
57 changes: 28 additions & 29 deletions source/ch4_conditionals.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -194,39 +194,38 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
<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.
</p>
<p><idx><c>switch</c> expressions</idx>
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>-&gt;</c> arrow syntax. These expressions can even use <c>yield</c> within code blocks for more complex evaluations. 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.
</p>




<p>
<idx><c>switch</c> expressions</idx>
<idx><c>yield</c></idx>
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>-&gt;</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.
</p>

<program interactive="activecode" language="java">
<code>
public class SwitchUp {
public static void main(String args[]) {
int grade = 85;
int tempgrade = grade / 10;
switch(tempgrade) {
case 10:
case 9:
System.out.println('A');
break;
case 8:
System.out.println('B');
break;
case 7:
System.out.println('C');
break;
case 6:
System.out.println('A');
break;
default:
System.out.println('F');
}
}
}
public class SwitchUp {
public static void main(String args[]) {
int grade = 85;
int tempgrade = grade / 10;
switch(tempgrade) {
case 10:
case 9:
System.out.println('A');
break;
case 8:
System.out.println('B');
break;
case 7:
System.out.println('C');
break;
case 6:
System.out.println('A');
break;
default:
System.out.println('F');
}
}
}
</code> <tests> </tests>
</program>

Expand Down