OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Switch Statement

 

Java switch Statement

The switch statement in Java is a multi-branch conditional statement that executes one block of code out of many options based on the value of a variable or expression.

It is an alternative to using multiple if-else-if statements when checking for equality.


Syntax of switch Statement


switch (expression) { case value1: // statements break; case value2: // statements break; ... default: // default statements }

🔑 Key Points:

  • The expression must evaluate to one of the following types:

    • byte, short, int, char, String, or an enum

  • case values must be unique constants or literals (not variables).

  • break prevents fall-through (execution of the next case).

  • default is optional and runs when no case matches.


🔸 Example 1: Simple Day Selector


public class SwitchExample1 { public static void main(String[] args) { int day = 3; switch (day) { 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; default: System.out.println("Invalid Day"); } } }

📤 Output:
Wednesday


🔸 Example 2: Switch with String


public class SwitchExample2 { public static void main(String[] args) { String fruit = "Mango"; switch (fruit) { case "Apple": System.out.println("Apple is red."); break; case "Banana": System.out.println("Banana is yellow."); break; case "Mango": System.out.println("Mango is sweet."); break; default: System.out.println("Unknown fruit."); } } }

📤 Output:
Mango is sweet.


🔸 Example 3: Switch without break (Fall-through Behavior)


public class SwitchExample3 { public static void main(String[] args) { int number = 2; switch (number) { case 1: System.out.println("One"); case 2: System.out.println("Two"); case 3: System.out.println("Three"); default: System.out.println("Default case"); } } }

📤 Output:


Two Three Default case

⚠️ Explanation: No break → executes all following cases.


Example 4: Calculator using switch


import java.util.Scanner; public class CalculatorSwitch { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter two numbers: "); double a = sc.nextDouble(); double b = sc.nextDouble(); System.out.print("Enter operation (+, -, *, /): "); char op = sc.next().charAt(0); switch (op) { case '+': System.out.println("Result = " + (a + b)); break; case '-': System.out.println("Result = " + (a - b)); break; case '*': System.out.println("Result = " + (a * b)); break; case '/': if (b != 0) System.out.println("Result = " + (a / b)); else System.out.println("Division by zero not allowed."); break; default: System.out.println("Invalid operator."); } sc.close(); } }


Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC