Skip to content

Commit 5cfe592

Browse files
authored
Merge pull request #163 from logananglin98/Issue#148Fix
Added yield definition
2 parents 301f73e + c2b00f5 commit 5cfe592

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

source/ch4_conditionals.ptx

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -194,39 +194,38 @@ Java also supports a <c>switch</c> statement that acts something like the <c>eli
194194
<p><idx><c>switch</c></idx>
195195
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.
196196
</p>
197-
<p><idx><c>switch</c> expressions</idx>
198-
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.
199-
</p>
200-
201-
202-
203197

198+
<p>
199+
<idx><c>switch</c> expressions</idx>
200+
<idx><c>yield</c></idx>
201+
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.
202+
</p>
204203

205204
<program interactive="activecode" language="java">
206205
<code>
207-
public class SwitchUp {
208-
public static void main(String args[]) {
209-
int grade = 85;
210-
int tempgrade = grade / 10;
211-
switch(tempgrade) {
212-
case 10:
213-
case 9:
214-
System.out.println('A');
215-
break;
216-
case 8:
217-
System.out.println('B');
218-
break;
219-
case 7:
220-
System.out.println('C');
221-
break;
222-
case 6:
223-
System.out.println('A');
224-
break;
225-
default:
226-
System.out.println('F');
227-
}
228-
}
229-
}
206+
public class SwitchUp {
207+
public static void main(String args[]) {
208+
int grade = 85;
209+
int tempgrade = grade / 10;
210+
switch(tempgrade) {
211+
case 10:
212+
case 9:
213+
System.out.println('A');
214+
break;
215+
case 8:
216+
System.out.println('B');
217+
break;
218+
case 7:
219+
System.out.println('C');
220+
break;
221+
case 6:
222+
System.out.println('A');
223+
break;
224+
default:
225+
System.out.println('F');
226+
}
227+
}
228+
}
230229
</code> <tests> </tests>
231230
</program>
232231

0 commit comments

Comments
 (0)