SlideShare a Scribd company logo
1 of 16
SWITCH-CASE
JAVA
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
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
}
• 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
}
• 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
}
• 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
}
• 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
}
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.
FLOWCHART
START
INPUT
SWITCH ()
Case
1
Case
2
?
?
? “Your input
is error”
STOP
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
}
• 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.
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
• 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.
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.
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();
}
}
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.

More Related Content

Similar to SWITCH-CASE, Lesson Computer Programming.pptx

Similar to SWITCH-CASE, Lesson Computer Programming.pptx (20)

What is Switch Case?
What is Switch Case?What is Switch Case?
What is Switch Case?
 
Vbscript
VbscriptVbscript
Vbscript
 
Md04 flow control
Md04 flow controlMd04 flow control
Md04 flow control
 
10. switch case
10. switch case10. switch case
10. switch case
 
lec 2.pptx
lec 2.pptxlec 2.pptx
lec 2.pptx
 
java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
 
Preparing Java 7 Certifications
Preparing Java 7 CertificationsPreparing Java 7 Certifications
Preparing Java 7 Certifications
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in c
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
QTP VB Script Trainings
QTP VB Script TrainingsQTP VB Script Trainings
QTP VB Script Trainings
 
Final requirement
Final requirementFinal requirement
Final requirement
 
Pj01 3-java-variable and data types
Pj01 3-java-variable and data typesPj01 3-java-variable and data types
Pj01 3-java-variable and data types
 
Android Application Development - Level 3
Android Application Development - Level 3Android Application Development - Level 3
Android Application Development - Level 3
 
Java chapter 2
Java chapter 2Java chapter 2
Java chapter 2
 
Vb (2)
Vb (2)Vb (2)
Vb (2)
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Kotlin programming language
Kotlin programming languageKotlin programming language
Kotlin programming language
 
Core Java
Core JavaCore Java
Core Java
 
Conditional Statement - Switch Case.pptx
Conditional Statement - Switch Case.pptxConditional Statement - Switch Case.pptx
Conditional Statement - Switch Case.pptx
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 

Recently uploaded

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

SWITCH-CASE, Lesson Computer Programming.pptx

  • 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.