Java Switch Statements

JK 2010 
Created at
Updated at  
7,379 0 0

Instead of writing many if..else statements, you can use the switch statement.

The switch statement selects one of many code blocks to be executed:

Syntax:

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

This is how it works:

  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.
  • The break and default keywords are optional, and will be described later in this chapter

 

The example below uses the weekday number to calculate the weekday name:

int day = 4;
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;
  case 5:
    System.out.println("Friday");
    break;
  case 6:
    System.out.println("Saturday");
    break;
  case 7:
    System.out.println("Sunday");
    break;
}
// Outputs "Thursday" (day 4)

The break Keyword

When Java reaches a break keyword, it breaks out of the switch block.

This will stop the execution of more code and case testing inside the block.

When a match is found, and the job is done, it's time for a break. There is no need for more testing.


The default Keyword

The default keyword specifies some code to run if there is no case match:

int day = 4;
switch (day) {
  case 6:
    System.out.println("Today is Saturday");
    break;
  case 7:
    System.out.println("Today is Sunday");
    break;
  default:
    System.out.println("Looking forward to the Weekend");
}
// Outputs "Looking forward to the Weekend"

Note that if the default statement is used as the last statement in a switch block, it does not need a break.

Tags Java Java switch break default switch Facebook X
Comments 0
Similar posts
  1. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    7,814
  2. Java Short Hand If...Else (Ternary Operator)
    7,546
  3. Java If ... Else
    7,368
  4. Java Math
    7,409
  5. Java Variables
    7,426
  6. Java Methods
    7,062
  7. Java Scope
    7,074
  8. Java Comments
    7,965
  9. Java Recursion
    7,182
  10. The Print() Method
    7,136
  11. Java Syntax
    7,413
  12. Java Classes and Objects
    7,061
  13. Java Getting Started
    7,383
  14. Java Packages
    7,159
  15. Java Inheritance (Subclass and Superclass)
    7,162
  16. Java Polymorphism
    7,036
  17. Java Inner Classes
    7,027
  18. What is Java?
    7,074
  19. Python While Loops/For Loops
    7,470
  20. Machine Learning Types and Programming Languages
    7,214
  21. Java Tutorials associated with AP Computer Science A
    7,562
  22. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,150
  23. Java Servlet Example
    7,102
  24. Difference between Java and Javascript
    7,574
  25. Challenge: One Code Problem Per Day
    1,049
  1. How do I replace content that based on the HTML UI Template
    7,138
  2. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,150
  3. Dataset of California Foodbanks
    7,152
  4. Java Tutorials associated with AP Computer Science A
    7,562
  5. Java Inner Classes
    7,027
  6. Java Polymorphism
    7,036
  7. Java Inheritance (Subclass and Superclass)
    7,162
  8. Java Packages
    7,159
  9. Java Abstract Classes and Methods
    7,676
  10. Java Classes and Objects
    7,061
  11. Java Recursion
    7,182
  12. Java Scope
    7,074
  13. Java Methods
    7,062
  14. Java Arrays
    8,627
  15. Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue
    7,814
  16. Java Short Hand If...Else (Ternary Operator)
    7,546
  17. Java If ... Else
    7,368
  18. Java Math
    7,409
  19. Java Variables
    7,426
  20. Java Comments
    7,965
  21. The Print() Method
    7,136
  22. Java Syntax
    7,413
  23. Java Getting Started
    7,383
  24. What is Java?
    7,074
Recently updated
  1. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    84
  2. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    205
  3. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    182
  4. The Evolution and Production Reality of Agentic AI
    174
  5. How to Activate or Waive Your UIUC Student Health Insurance
    253
  6. Complete Guide to Building a Machine Learning Model
    295
  7. My life cuts at Las Vegas during Thanksgiving day holiday
    7,376
  8. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    319
  9. Harness vs. OpenClaw: Two Very Different "Agents"
    912
  10. Clean Python Environments: The Power of venv vs. Docker
    784
  11. What is Docker? Why is Docker also useful in a development environment?
    591
  12. UIUC 2026-2027 Academic Calendar
    1,505
  13. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    771
  14. Open-Source LLMs: The AI Revolution
    726
  15. Resume 2.0: Leveling Up for My First Software Gig
    2,116
  16. Not everyone will understand what this man just did
    1,748
  17. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,554
  18. Unpacking IU's Shopper
    719
  19. Jackie Chan's Police Story: The Action Masterpiece
    616
  20. The IVE Story: Identity, 'I AM' Charts, and Influence
    916
  21. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,154
  22. Open Databases for Sex Crime Occurrences in the U.S.
    687
  23. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,546
  24. My First Day at University of Illinois-Urvana Champaign
    1,161
  25. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,094
  26. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,169
  27. Sipping the Stars: My Starbucks Adventure
    9,593
  28. Exciting explore at Sequoia National Park
    7,624
  29. My Life Shot at Death Valley
    1,648
  30. Ip Man fights with Muay Thai Master
    910
  31. Mad Clown - Don't Die
    982
  32. How to get Student Enrollment and Degree Verification at UIUC
    4,687
  33. LAX Thanksgiving Rush: A Joyful Reunion
    901
  34. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,087
  35. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    952
  36. 10CM - To Reach You (너에게 닿기를)
    1,133
  37. Feeling weak? Transform yourself at the UIUC ARC!
    1,551
  38. BOYNEXTDOOR - If I Say I Love You
    1,163
  39. The Future of Software Engineer - AI Engineering
    914
  40. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    886
  41. Lie - Legend song by BIGBANG
    7,790
  42. Why ROLLBACK is useful when you work with Google Gemini CLI?
    808
  43. Reimbursement after Vaccination at McKinley Health Center
    986
  44. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    941
  45. Common Questions from UIUC school life in terms of CS Program
    1,083
  46. UIUC Immunization Compliance
    1,176
  47. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,071
  48. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,072
  49. Cupid (OT4/Twin Ver.) - LIVE IN STUDIO | FIFTY FIFTY (피프티피프티)
    847
  50. Common methods to improve coding skills
    969