# Switch Case in Dart

* A ***switch*** statement is an alternative to ***elseif statements*** which allows a variable to be tested for equality against a list of values.
    
* Each value is called a ***case***, and the variable being switched on is checked for each switch case.
    
* Wherever an expression value matches with a case value, the body of that case will be executed.
    
* The switch will be terminated using a ***break*** statement. Here break statement is compulsory. Otherwise, the dart analysis engine will throw a syntax error.
    
* Only the default case break is optional. Otherwise, in all cases break is compulsory.
    

### Syntax

```javascript
switch (expression) {
  case ONE:
    {
      statement(s);
    }
  break;
  
  case TWO:
    {
      statement(s);
    }
  break;
  
  default:
    {
      statement(s);
    }
}
```

### Rules of the Switch Case

* The *default* case is optional.
    
* All case expressions must be unique.
    
* The case statements can include only constants. It cannot be a variable or an expression.
    
* The data type of the variable and the case expression must match.
    
* There can be any number of case statements within a switch.
    

### Example

```javascript
void main() {
  var grade = "A";
  
  switch (grade) {
    case "A":
      {
        print("Excellent");
      }
      break;

    case "B":
      {
        print("Good");
      }
      break;

    case "C":
      {
        print("Fair");
      }
      break;

    case "D":
      {
        print("Poor");
      }
      break;

    default:
      {
        print("Invalid choice");
      }
      break;
  }
}
```

So, guys, That’s all you need to know about switch cases. Please let me know if I miss something. I’ll be happy to learn from you. Till Then Keep Loving, Keep Coding. I’ll surely catch you up in the next article. Jai Hind, Vande Mataram 🇮🇳

> Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitate that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Subscribe To My Newsletter For More Such Content.</div>
</div>

### **Learn More about Dart and Flutter**

* [List in Dart](https://jaytillu.com/blogs/list-in-dart)
    
* [Abstract Class and Abstract Method in Dart](https://jaytillu.com/blogs/abstract-classes-and-abstract-methods-in-dart)
    
* [Inheritance in Dart](https://jaytillu.com/blogs/interface-in-dart)
    
* [Constructors in Dart](https://jaytillu.com/blogs/constructors-in-dart)
    
* [Classes and Objects in Dart](https://jaytillu.com/blogs/classes-and-objects-in-dart)
    
* [Arrow Functions in Dart](https://jaytillu.com/blogs/arrow-functions-in-dart)
    
* [User-Defined Functions in Dart](https://jaytillu.com/blogs/user-defined-function-in-dart)
    
* [Functions in Dart](https://jaytillu.com/blogs/functions-in-dart)
    
* [Switch Case in Dart](https://jaytillu.com/blogs/switch-case-in-dart)
    
* [Conditionals in Dart](https://jaytillu.com/blogs/conditionals-in-dart)
    
* [Operators in Dart](https://jaytillu.com/blogs/operators-in-dart)
    
* [Keywords in Dart](https://jaytillu.com/blogs/keywords-in-dart)
    
* [Variables in Dart](https://jaytillu.com/blogs/variables-in-dart)
    
* [Data Types in Dart](https://jaytillu.com/blogs/data-types-in-dart)
    
* [Dart SDK](https://jaytillu.com/blogs/dart-sdk)
    

### **Follow me for more such content**

* [My Site](https://www.jaytillu.com/)
    
* [My Blogs](https://jaytillu.com/blogs)
    
* [LinkedIn](https://www.linkedin.com/in/jaytillu/)
    
* [Instagram](https://www.instagram.com/jay.tillu/)
    
* [Twitter](https://twitter.com/jay_tillu)
    
* [Stackoverflow](https://stackoverflow.com/users/8509590/jay-tillu)
    
* [Github](https://github.com/Jay-Tillu)
    
* [Dev](https://dev.to/jay_tillu)
    
* [Medium](https://jaytillu.medium.com/)
    
* [My YouTube Channel](https://www.youtube.com/@jay_tillu)
