2. SWITCH-CASE JAVA
• In Java, a switch-case statement lets you run different
parts of your code depending on what a variable's value
is. It's like having a menu with different options, and
depending on what you pick, you go down a different
path. This makes your code organized and easier to
understand, especially when you have lots of choices or
conditions to consider.
•Selection
3. Unsa ang mga data type nga
pwede magamit sa switch?
• int: Gamit ang integers (bilang).
Example:
int choice = 1;
switch (choice) {
case 1:
// Code for case 1
break;
case 2:
// Code for case 2
break;
default:
// Default code
}
4. • char: Gamit ang characters (titik).
Example:
char grade = 'A’;
switch (grade) {
case 'A':
// Code for case A
break;
case 'B':
// Code for case B
break;
default:
// Default code
}
5. • byte: Gamit ang mga byte na integers.
Example:
byte number = 3;
switch (number) {
case 1:
// Code for case 1
break;
case 2:
// Code for case 2
break;
default:
// Default code
}
6. • short: Gamit ang mga maikling integers.
Example:
short month = 9;
switch (month) {
case 1:
// Code for January
break;
case 2:
// Code for February
break;
// and so on...
default:
// Default code
}
7. • String: Gamit ang mga string (mga salitang kadena).
Example:
String fruit = "Apple";
switch (fruit) {
case "Apple":
// Code for case Apple
break;
case "Banana":
// Code for case Banana
break;
default:
// Default code
}
8. Kailangan ang mga data type na ito ay mga
primitive data types o object references, at HINDI
PWEDENG GAMITIN ANG MGA
FLOAT, DOUBLE, o LONG sa SWITCH
STATEMENT.
10. SYNTAX OF SWITCH
switch (expression) {
case value1:
// code to be executed if expression matches value1
break;
case value2:
// code to be executed if expression matches value2
break;
// more cases can be added as needed
default:
// code to be executed if expression doesn't match any case
}
11. • expression: This is the value that the switch statement evaluates. It
can be of type byte, short, int, char, String, or an enumerated type.
• case value: Each case is followed by a value that the expression
may match. If the expression matches a case value, the
corresponding block of code will be executed. If multiple case values
have the same block of code to execute, you can stack the cases without
breaking.
• break: After the code block for a case is executed, the break
statement is used to exit the switch statement. Without the break, the
control will flow to the next case and continue executing code until a
break statement is encountered.
• default: This is an optional case. If none of the case values match the
expression, the code within the default block is executed. It serves
as a catch-all.
12. FACTS OF SWITCH CASE JAVA
• Supported Data Types: In Java, the switch statement can be used with
data types byte, short, int, char, String, and enumerated types (enum).
• Expression Matching: The switch statement evaluates an expression and
compares it with the values specified in case labels. When a match is found, the
corresponding block of code is executed.
• Exact Match Requirement: Unlike some other languages, Java requires
an exact match between the expression value and the case value. There's no
implicit fall-through in Java's switch statement. Each case block needs to end with
a break statement, otherwise, subsequent case blocks will be executed until a
break statement is encountered or the end of the switch block is reached.
• Multiple Labels: You can have multiple case labels for a single block of code
13. • Default Case: The default case is optional and executes if none of the
case labels match the expression value.
• Switch with Strings: Starting from Java 7, you can use String in a
switch statement. Prior to Java 7, only numeric types and char were
allowed.
• No Type Checking: Unlike if-else statements, switch does not
perform type checking. So, for example, if you pass null to a switch
statement, it will result in a NullPointerException.
• Fall-through: If a case block does not end with a break statement,
control falls through to the next case block. This can be used intentionally
to execute multiple case blocks for the same code. However, it's often
considered a bad practice because it can lead to unintended behavior and
makes the code less readable.
14. SAMPLE PROBLEM
1. Input an integer number from 1 to 5. Then print the corresponding
working day of the week, 1 being "MONDAY", 2 -"TUESDAY",... Display
an error message if the value entered is not in the range.
15. import java.util.Scanner;
public class DayOfWeek {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number from 1 to
5: ");
int dayNumber = scanner.nextInt();
switch (dayNumber) {
case 1:
System.out.println("MONDAY");
break;
case 2:
System.out.println("TUESDAY");
break;
case 3:
System.out.println("WEDNESDAY");
break;
case 4:
System.out.println("THURSDAY");
break;
case 5:
System.out.println("FRIDAY");
break;
default:
System.out.println("Error: Input is not in
the range of 1 to 5.");
}
scanner.close();
}
}
16. Problems
1. Draw a flowchart and write a program that takes an integer input
representing a month number (1 for January, 2 for February, etc.)
and prints the corresponding month name.
2. Create a basic calculator that takes two numbers and an operator
(+, -, *, /) as input, then performs the corresponding arithmetic
operation and draw a flowchart.
3. Given a number representing a day of the week (1 for Monday, 2 for
Tuesday, etc.), write a program that prints whether it's a weekday or
a weekend and draw its flowchart.