Skip to main content

Switch Case

Syntax : Switch Case in Java Programming

  1. It is alternative to else-if ladder.
  2. Switch Case Syntax is similar to – C/C++  Switch.
  3. Switch allows you to choose a block of statements to run from a selection of code, based on the return value of an expression.
  4. The expression used in the switch statement must return an int, a String, or an enumerated value.
switch (selection) {  // value
case value1 :         // checking value 1
     statement(s);
     break;           // use to break switch flow if condition match
case value2 :         // checking value 2
     statement(s);
     break;
 .
 .
case value_n :
     statement(s);
     break;
default:
     statement(s);
}

Different Ways of Using Switch Case :

Switch Case Using Integer Case

int i=3; 
switch (i) {
case 1 :
     System.out.println("One player is playing this game.");
     break;
case 2 :
     System.out.println("Two players are playing this game.");
     break;
case 3 :
     System.out.println("Three players are playing this game.");
     break;
default:
     System.out.println("You did not enter a valid value.");
}

OutPut :

Three players are playing this game.

Switch Using String

String name = "Zubair";

switch (name) {

case "ALI" :
     System.out.println("ALI is playing this game.");
     break;

case "Zubair" :
     System.out.println("Zubair is playing this game.");
     break;

case "Ubaid" :
     System.out.println("Ubaid is playing this game.");
     break;

default:
     System.out.println("You did not enter a valid name.");
}

OutPut :

Zubair is playing this game.
  • If you are coming from C/C++ then you must understand this concept that – Java Supports String Inside Case Label.
  • Above program if used in C/C++ then it will give us compile time error.

How Switch Case Statement is different in java from c programming

  1. In C Programming we were unable to write Case label of data type  ‘String’. In java we can have String inside “Switch”.

Example  : Choose Day of the Week

class SwitchDate{
  public static void main(String[] args){
  int week = 3;
  switch (week){
      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 week");
            break;
      }
  }
}

Output :

wednesday


Comments

  1. Nicely explained. Specially those examples. I have also wrote about Switch Case Check out here

    ReplyDelete
  2. material proved to be really efficient and beneficial to me. Thank you very much for providing this information ..visit here Power Apps development company in Canada / power apps consultation canada

    ReplyDelete
  3. Thanks for this wonderful information We specialise in Websites, marketing, online business development & cloud based systems. We Help Businesses Grow through Digital Marketing and Business Development Services

    ReplyDelete
  4. Thank you for sharing the article. Looking for digital transformation services?
    The data that you provided in the blog is informative and effective.I am happy to visit and read useful articles here. I hope you continue to do the sharing through the post to the reader. For more detail visit Trans Tech Solutions .

    ReplyDelete
  5. nice blog! also join us on https://it34.alhuda.com.pk/

    ReplyDelete
  6. Nice Post….!!!! Greps Ai specializes in transforming businesses with cutting-edge technology solutions. Leveraging expertise in Digital Marketing, Chatbot Development, API Development, and Software Development Designing, Greps Ai empowers companies to achieve exponential growth. By integrating AI-driven strategies and innovative software, Greps Ai for Business Growth ensures scalable solutions tailored to meet diverse business needs, fostering efficiency and competitive advantage in today's dynamic market landscape.

    ReplyDelete

Post a Comment

Popular posts from this blog

Inheritance in Java

Inheritance in Java Inheritance  is one of the feature of Object-Oriented Programming (OOPs). Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as super-class . The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class. Inheritance is a  compile-time  mechanism. A super-class can have any number of subclasses . But a subclass can have only one superclass. This is because Java does not support multiple inheritance. The superclass and subclass have  “is-a”  relationship between them. Let’s have a look at the example below. Inheritance  Example Let’s consider a superclass  Vehicle . Different vehicles have different features and properties howeve...

Multilevel inheritance

Multilevel inheritance We discussed a bit about  Multilevel inheritance  in types of inheritance in java. In this tutorial we will explain multilevel inheritance with the help of   diagram  and  example program . It’s pretty clear with the diagram that in Multilevel inheritance there is a concept of grand parent class. If we take the example of above diagram then class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B. So in this case class C is implicitly inheriting the properties and method of class A along with B that’s what is called multilevel inheritance. Example : In this example we have three classes –  Car, Maruti and Maruti800. We have done a setup – class Maruti extends Car and class Maurit800 extends Maurti. With the help of this Multilevel hierarchy setup our Maurti800 class is able to use the methods of both the classes (Car and Maruti). class Car { ...