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 <iostream>
using namespace std;
int main() {
  int month_no = 4;
  switch(month_no) 
  {
    case 1:
      cout << "Month : January";
      break;
    case 2:
      cout << "Month : February";
      break;
    case 3:
      cout << "Month : March";
      break;
    case 4:
      cout << "Month : April";
      break;
    case 5:
      cout << "Month : May";
      break;
    default:
      cout << "Month after the May..";
  }
  return 0;
}
Output
Month : April

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

string fruit = "Apple";
switch(fruit){
   // case...
} 

error: invalid conversion from 'const char*' to 'int'
c++ switch without break

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

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;
int main() {
  int month_no = 4;
  switch(month_no) 
  {
    case 1:
      cout << "Month : January";
    case 2:
      cout << "Month : February";
    case 3:
      cout << "Month : March";
    case 4:
      cout << "Month : April";
    case 5:
      cout << "Month : May";
    default:
      cout << "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