From c2b00f50f2dc84dc0207c1a127ff1afbbde75462 Mon Sep 17 00:00:00 2001 From: logananglin98 Date: Wed, 13 Aug 2025 11:30:21 -0400 Subject: [PATCH] Added a one-sentance definition for yield and added it to the index. --- source/ch4_conditionals.ptx | 57 ++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/source/ch4_conditionals.ptx b/source/ch4_conditionals.ptx index 6b7a86b..5720ee1 100644 --- a/source/ch4_conditionals.ptx +++ b/source/ch4_conditionals.ptx @@ -194,39 +194,38 @@ Java also supports a switch statement that acts something like the eli

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

-

switch expressions - Java 14 introduced switch expressions, enhancing functionality by allowing the switch to return values and eliminating fall-through via the -> arrow syntax. These expressions can even use yield within code blocks for more complex evaluations. It’s important to note that traditional switch statements do not support null values and will throw a NullPointerException if evaluated with null. As the language evolves, newer versions of Java continue to extend switch capabilities with features like pattern matching and enhanced type handling, making it a more powerful and expressive tool for decision-making in Java programs. -

- - - +

+ switch expressions + yield + Java 14 introduced switch expressions, enhancing functionality by allowing the switch to return values and eliminating fall-through via the -> arrow syntax. These expressions can even use yield within code blocks for more complex evaluations. yield is used inside a switch expression’s block to produce the value of that expression, unlike break which simply exits a switch statement or loop. It’s important to note that traditional switch statements do not support null values and will throw a NullPointerException if evaluated with null. As the language evolves, newer versions of Java continue to extend switch capabilities with features like pattern matching and enhanced type handling, making it a more powerful and expressive tool for decision-making in Java programs. +

-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'); + } + } + }