Switch statements

इंटरमीडिएट Java

Jim White

Java Developer

Switch क्या है?

  • Switch का उद्देश्य if-else जैसा होता है
    • किसी condition के आधार पर कुछ actions करता है
    • कई if-then-else की जगह उपयोग होता है
  • कई if-then-else से अधिक संक्षिप्त माना जाता है
if (bonus == 10000) {
    System.out.println("Let's buy a car!");
} else if (bonus == 5000) {
    System.out.println("Let's take a trip!");
} else if (bonus == 1000) {
    System.out.println("Let's save!");
}
इंटरमीडिएट Java

Switch सिंटैक्स

switch (expression) {
    case value1:  // if (expression == value1)
        // statements
    case value2:  // if (expression == value2)
        // statements
    // use as many case statements as needed
}
इंटरमीडिएट Java

Switch उदाहरण

char direction = 'N';

switch (direction) {
    case 'E':  
        System.out.println("We are headed east.");
    case 'S':  
        System.out.println("We are headed south.");
    case 'W':  
        System.out.println("We are headed west.");
    case 'N':   
        System.out.println("We are headed north."); // This would be printed
}
इंटरमीडिएट Java

Break

  • Java को बाकी cases पर आगे बढ़ने से रोकता है
    • break न हो तो Java अगले case के statements चलाएगा, जब तक break न मिले
char direction = 'W';

switch (direction) {
    case 'E':
        System.out.println("We are headed east.");
    case 'S':
        System.out.println("We are headed south.");
    case 'W':
        System.out.println("We are headed west."); // This would be printed
    case 'N':
        System.out.println("We are headed north."); // This would also be printed
}
इंटरमीडिएट Java

break कई cases को साथ जोड़ने देता है:

char direction = 'N';

switch (direction) {
    case 'E':     
    case 'W':  
        System.out.println("We are headed east or west.");
        break;
    case 'S':  
    case 'N':
        System.out.println("We are headed south or north.");
        break;       
}
We are headed south or north.
इंटरमीडिएट Java

Default

  • switch के अंत में वैकल्पिक default case जोड़ सकते हैं
    • default case में expression से तुलना करने को कोई मान नहीं होता
    • यदि कोई case expression से न मिले, तो default statements चलेंगे
    • default switch में आखिरी case होना चाहिए
  • default "बाकी सभी मान" के लिए case देता है
char direction = 'z';

switch (direction) {
  case 'E':     
  case 'W':  
    System.out.println("To east or west.");
    break;
  case 'S':  
  case 'N':
    System.out.println("To south or north.");
    break; 
  default:
    System.out.println("We are lost.");
}
We are lost.
इंटरमीडिएट Java

Conditionals बनाम switch

  • वही काम conditionals से भी हो सकता है
  • हर data type के साथ काम करते हैं
char character = 'A';
if (character == 'A') {
    System.out.println("It's A");
} else if (direction =='B') {
    System.out.println("It's B");
} else {
    System.out.println("Unknown");
}
  • Switch पढ़ने में आसान हो सकता है
  • केवल कुछ data types के साथ काम करता है, जैसे int, byte, short, char, या String
char character = 'A';
switch (character) {
  case 'A':
    System.out.println("It's A");
    break;
  case 'B':  
    System.out.println("It's B");
    break;
  default:
    System.out.println("Unknown");
}
इंटरमीडिएट Java

अभ्यास करते हैं!

इंटरमीडिएट Java

Preparing Video For Download...