Java switch-case In Hindi

📔 : Java 🔗

Java में switch loop, किसी matched expression के लिए code of block Run करता है , यह लगभग else if की तरह ही work करता है जहा हम कई सारी conditions में से true condition वाला statement ही run होता था, और अगर एक भी condition match नहीं होती तो else part (default) run होता था।


switch में हम case clause use करते हैं , और जो case expression से match करता है वही statement execute करता है। और कोई case match न होने पर default statement execute होता है।

Java switch Syntax

switch (expression) 
{
    case valueN:
      // code of block
      break;
    case valueN:
      // code of block
      break;
    default:
      // default case
}
  • expression - दिया गया expression , case में दी गयी value से match होता है।

  • case clause - एक switch loop में कितने ही case clause हो सकते हैं , और case में दी जाने वाली value दिए गए expression से match करती है अगर expression match होता है ,तो उस case से associated code of block रन हो जाता है।

  • break - expression match होने पर उस case से associated code of block run होने के बाद break statement switch loop को terminate करता है।

  • default - अगर कोई भी expression match नहीं होता है तो default statement run होता है।

Java switch Example

File : SwitchExample.java

CopyFullscreenClose FullscreenRun
public class SwitchExample {
  public static void main(String[] args) {
    int month_no = 4;
    switch(month_no) {
      case 1:
        System.out.println("Month : January");
        break;
      case 2:
        System.out.println("Month : February");
        break;
      case 3:
        System.out.println("Month : March");
        break;
      case 4:
        System.out.println("Month : April");
        break;
      case 5:
        System.out.println("Month : May");
        break;
      default:
        System.out.println("Month after the May..");
    }
  }
}
Output
javac SwitchExample.java
java SwitchExample
Month : April

Note : Java programmming language में ध्यान रहे कि आप , जिस data type की value pass कर रहे हैं switch case clause में match होने वाली values भी उस type की होनी चाहिए। ऐसा नहीं हो सकता है कि match की जाने वाली values integer हैं और switch में आप String value pass कर रहे हैं , ऐसा करने पर error आएगी।

File : SwitchExample.java

CopyFullscreenClose FullscreenRun
public class SwitchExample {
  public static void main(String[] args) {
    String month = "Jan";
    switch(month) {
      case "Jan":
        System.out.println("Month : January");
        break;
      case 1: // here we've taken an integer value.
        System.out.println("Month : January");
        break;
      default:
        System.out.println("Month after the May..");
    }
  }
}
Output
javac SwitchExample.java
java SwitchExample
SwitchExample.java:8: error: incompatible types: int cannot be converted to String
      case 1: // here we've taken an integer value.
           ^
1 error

Java switch without break

अगर आप break statement का use नहीं करते हैं तो , जिस case से condition match होती है , वहां से सभी cases का block of code run होता है।

File : SwitchExample.java

CopyFullscreenClose FullscreenRun
public class SwitchExample {
  public static void main(String[] args) {
    int month_no = 3;
    switch(month_no) {
      case 1:
        System.out.println("Month : January");
      case 2:
        System.out.println("Month : February");
      case 3:
        System.out.println("Month : March");
      case 4:
        System.out.println("Month : April");
      case 5:
        System.out.println("Month : May");
      default:
        System.out.println("Month after the May..");
    }
  }
}
Output
javac SwitchExample.java
java SwitchExample
Month : March
Month : April
Month : May
Month after the May..

default case in Java switch-case

जैसा कि आपने ऊपर पढ़ा कि अगर कोई case match नहीं होता है तो default case हो run होता है।

File : SwitchExample.java

CopyFullscreenClose FullscreenRun
public class SwitchExample {
  public static void main(String[] args) {
    int month_no = 3;
    switch(month_no) {
      case 1:
        System.out.println("Month : January");
        break;
      case 2:
        System.out.println("Month : February");
        break;
      default:
        System.out.println("Default case is running.");
    }
  }
}
Output
javac SwitchExample.java
java SwitchExample
Default case is running.

I Hope, अब आप java में switch case के बारे में अच्छे से समझ गए होंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers