Java Switch Statements

JK JK 2038 
Created at
Updated at  
7,496 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
    8,022
  2. Java Short Hand If...Else (Ternary Operator)
    7,667
  3. Java If ... Else
    7,496
  4. Java Math
    7,520
  5. Java Variables
    7,564
  6. Java Methods
    7,158
  7. Java Scope
    7,186
  8. Java Comments
    8,188
  9. Java Recursion
    7,250
  10. The Print() Method
    7,219
  11. Java Syntax
    7,542
  12. Java Classes and Objects
    7,184
  13. Java Getting Started
    7,490
  14. Java Packages
    7,242
  15. Java Inheritance (Subclass and Superclass)
    7,240
  16. Java Polymorphism
    7,124
  17. Java Inner Classes
    7,144
  18. What is Java?
    7,151
  19. Python While Loops/For Loops
    7,613
  20. Machine Learning Types and Programming Languages
    7,323
  21. Java Tutorials associated with AP Computer Science A
    7,754
  22. Creating a simple Java Servlet (Web Server Page) with Apache Maven on Microsoft Windows
    7,250
  23. Java Servlet Example
    7,181
  24. Difference between Java and Javascript
    7,726
  25. Challenge: One Code Problem Per Day
    1,253
  1. How do I replace content that based on the HTML UI Template
    7,216
  2. Dataset of California Foodbanks
    7,232
  3. Java Abstract Classes and Methods
    7,862
  4. Java Arrays
    8,971
Recently updated
  1. Comprehensive Guide to Recent Critical PHP Vulnerabilities
    628
  2. How to Check Your UIUC Graduation Progress Using DARS Audit
    550
  3. Michael Jackson's Billie Jean
    352
  4. Meet A2O Lite: Korea's Newest K-Pop Cuties with Addictively Catchy Melodies
    266
  5. Why You Can't Stop Watching Jack Bryan's Beatbox Gaming Shorts
    267
  6. Song So-hee: The Soulful Voice Touching Hearts Through Korean Music
    306
  7. Watch This Dazzling Dancer’s Mind-Blowing Moves
    320
  8. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    347
  9. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    1,073
  10. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    500
  11. The Evolution and Production Reality of Agentic AI
    390
  12. How to Activate or Waive Your UIUC Student Health Insurance
    490
  13. Complete Guide to Building a Machine Learning Model
    510
  14. My life cuts at Las Vegas during Thanksgiving day holiday
    7,578
  15. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    531
  16. Harness vs. OpenClaw: Two Very Different "Agents"
    1,291
  17. Clean Python Environments: The Power of venv vs. Docker
    1,060
  18. What is Docker? Why is Docker also useful in a development environment?
    778
  19. UIUC 2026-2027 Academic Calendar
    1,912
  20. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    1,027
  21. Open-Source LLMs: The AI Revolution
    946
  22. Resume 2.0: Leveling Up for My First Software Gig
    2,806
  23. Not everyone will understand what this man just did
    2,316
  24. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,800
  25. Unpacking IU's Shopper
    947
  26. Jackie Chan's Police Story: The Action Masterpiece
    824
  27. The IVE Story: Identity, 'I AM' Charts, and Influence
    1,148
  28. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,352
  29. Open Databases for Sex Crime Occurrences in the U.S.
    919
  30. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    3,210
  31. My First Day at University of Illinois-Urvana Champaign
    1,349
  32. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,364
  33. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,442
  34. Sipping the Stars: My Starbucks Adventure
    10,022
  35. Exciting explore at Sequoia National Park
    7,837
  36. My Life Shot at Death Valley
    1,985
  37. Ip Man fights with Muay Thai Master
    1,103
  38. Mad Clown - Don't Die
    1,220
  39. How to get Student Enrollment and Degree Verification at UIUC
    5,603
  40. LAX Thanksgiving Rush: A Joyful Reunion
    1,060
  41. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,318
  42. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    1,126
  43. 10CM - To Reach You (너에게 닿기를)
    1,311
  44. Feeling weak? Transform yourself at the UIUC ARC!
    1,717
  45. BOYNEXTDOOR - If I Say I Love You
    1,344
  46. The Future of Software Engineer - AI Engineering
    1,070
  47. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    1,070
  48. Lie - Legend song by BIGBANG
    7,969
  49. Why ROLLBACK is useful when you work with Google Gemini CLI?
    1,046
  50. Reimbursement after Vaccination at McKinley Health Center
    1,175