Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Java Switch Case : Switch fall-through, default, break, examples

  • Rabindranath Tagore Biography in Hindi, Poems, Birthday
    Rabindranath Tagore Biography in Hindi, Title of Gurudev Biography
  • Ramayan : Short Story of ramayana in Hindi | Qualities of Rama
    Ramayan : Short Story of ramayana in Hindi | Qualities of Rama Spiritual
  • Famous Slogans given by famous Personalities Indian leaders
    Famous Slogans of freedom fighters in Hindi | स्वतंत्रता सेनानियों के प्रसिद्ध नारे हिंदी में General Knowledge
  • Amitabh Bachchan biography in hindi | Family, wife, awards
    Amitabh Bachchan biography in hindi | Family, wife, awards Biography
  • Chemical Properties of Matter (Element, Compound and Mixture)
    Chemical Properties of Matter examples (Element, Compound and Mixture) Science
  • Mahatma Gandhi Essay in Hindi | Gandhiji Biography
    Mahatma Gandhi Essay in Hindi | Gandhiji Biography Biography
  • Kishkindha Kand in Hindi | Ram meets Hanuman | किष्किंधा कांड
    Kishkindha Kand in Hindi | Ram meets Hanuman | किष्किंधा कांड Spiritual
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
    Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी Biography

Java Switch Case : Switch fall-through, default, break, examples

Posted on August 21, 2021June 17, 2022 By GeekCer Education No Comments on Java Switch Case : Switch fall-through, default, break, examples
Java Switch Case

We use Java Switch case when we have multiple options and we have to choose only one option from the available options. You can perform particular task depending on the selected option.

The switch case statement is an alternative to multiple if-else conditions, because multiple if-statements are a very time-consuming process.

A switch statement works with byte, short, char and int primitive data types. It also works with enumerated types and String classes.

Table of Contents

  • Syntax of Java Switch Case
  • How does the switch-case statement work?
  • Example of Java Switch case
    • What is the use of break keyword in switch?
    • What is the use of default keyword in switch?
    • Java Switch fall-through example

Syntax of Java Switch Case

This is the syntax of the java switch statement


switch (variable) {
  case value1:
    // Code block to be executed if expression is equal to value1
    break;
  case value2:
     // Code block to be executed if expression is equal to value2
    break;
  case value3:
     // Code block to be executed if expression is equal to value3
    break;
  default:
     // Default statement
}

The particular code block will execute based on the value of the variable. If the value of variable is equal to value1, then code block of value1 will execute. And If the value of variable is equal to value2 then the code block of value2 will execute and so on.

If the value of the variable is not equal to value1, value2, etc., the program control goes to the default section and the code block of the default statement will execute.

How does the switch-case statement work?

Take a look at the switch case syntax above. Switch is a variable within a parameter, and we’ve used value1, value2, and value3 with case.

  • Each case’s values (value 1, value 2, and value 3) are compared to the value of variable.
  • The code for case value1 is run if the variable has the value1. In the same way, if the variable matches the value2, the case value2 code is run and so on.
  • If no match is found, the code for the default case is used.

Example of Java Switch case


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

We mostly use switch statements in menu driven programs where we show a list of items and the user can select an item from the list of items available in the menu.

What is the use of break keyword in switch?

In java program we use the break statement inside the switch blocks to come out of the switch block. Break statements are necessary because without them, the statements in the switch block fall through. The break statement ignores the execution of other code in the switch block.

What is the use of default keyword in switch?

In switch statement, there may be possibility that some section switch case does not handles, in this case the default statement handles that section.

We can use default statement at any place inside the switch but we should use in the last statement in a switch block. Default statement does not need a break.

Java Switch fall-through example

Let’s take a look at the fall-through program, when we don’t use break statement in some cases, so that you can understand better about fall-through


public class SwitchDemo {
  public static void main(String[] args) {
    int day = 1;
    switch (day) {
      case 1:
        System.out.println("Monday");
      case 2:
        System.out.println("Tuesday");
      case 3:
        System.out.println("Wednesday");
      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;
      default:
        System.out.println("Invalid day");
    }
  }
}

Output:
Monday
Tuesday
Wednesday
Thursday

In conclusion, You have learned about the switch statement, syntax of switch statement, java switch fall-through and the example.

Switch statement is a very important section in Java programming for beginners and experience, for interview purpose.

Recommended Articles

  • List of programs in Java
  • Thread Life cycle and Thread creation in Java
  • Simple example of thread synchronization in java
  • When to use java streams?
  • Find String List from List of Objects using Java 8 Streams
  • Functional interfaces and lambda expressions

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java Tags:break, byte, Case, Char, default, fall-through, Int, Primitive Type, Short, Switch

Post navigation

Previous Post: Java If-else Statement
Next Post: Loop in Java, Looping statements in java (for, while, do..while)

More Related Articles

Difference between Comparable and Comparator Difference between Comparable and Comparator Differences
Java Operators Java Operators Java
Sequential and Parallel Stream in Java | Example, Differences Sequential and Parallel Stream in Java | Example, Differences Differences
Immutable class in Java Immutable class in Java | How to create Immutable class in Java Important
Java 11 new Features (With example Programs) Java 11 new Features (With example Programs) Important
Java Vector Vector in Java Collection, Constructors, Example Programs Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java Home
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Keywords
  • Java Operators
  • Java If-else Statement
  • Java Switch
  • Java Loop
  • Java Arrays
  • Method Overloading in Java
  • Java OOP
  • Java Collections
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Multithreading in java
  • Thread Synchronization
  • Exception Handling
  • Java JDBC Driver
  • Java Database Connectivity steps
  • Lambda Expressions
  • Concurrent Collections
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved