C switch Loop In Hindi

📔 : C 🔗

C में 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 होता है।

C switch loop 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 करता है। learn more about break . . .

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

C switch loop example

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  int month_no = 4;
  switch(month_no) 
  {
    case 1:
      printf("Month : January");
      break;
    case 2:
      printf("Month : February");
      break;
    case 3:
      printf("Month : March");
      break;
    case 4:
      printf("Month : April");
      break;
    case 5:
      printf("Month : May");
      break;
    default:
      printf("Month after the May..");
  }
  return 0;
}
Output
Month : April

Note : C में switch loop में cases को सिर्फ और सिर्फ numeric data type values जैसे Integer , Double , Float value ही match करा सकते हैं Character, String match नहीं करा सकते हैं। Even string match कराने पर कुछ इस तरह की error आएगी।

char fruit[] = "orange";
switch(fruit) 
{
  case "orange" : 
    printf("Matching");
    break;
  default:
    printf("default case");
}

// Output :

error: switch quantity not an integer
4 |   switch(fruit)
	|          ^~~~~
error: case label does not reduce to an integer constant
6 |     case "orange" :
	|     ^~~~

C switch without break

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

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  int month_no = 4;
  switch(month_no) 
  {
    case 1:
      printf("Month : January");
    case 2:
      printf("Month : February");
    case 3:
      printf("Month : March");
    case 4:
      printf("Month : April");
    case 5:
      printf("Month : May");
    default:
      printf("Month after the May..");
  }
  return 0;
}
Output
Month : AprilMonth : MayMonth after the May..

example में आप clearly देख सकते हैं , कि जहाँ से case match हुआ है वहां से सभी cases automatically run हुए हैं , इसलिए switch loop use करते समय break का use करना न भूलें।

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