C – Case control statements (part-1) switch-case statement | W4 learn in hindi

C case control statement क्या होते है?

case control statement in C 

दोस्तों आज हम  " learnw4 के  C series" के tutorial में सीखेंगे कि case control statementक्या होता है  
तो चलिये शुरू करते है   W4 learn in Hindi के साथ,

case control statement in C


block की एक series में statement के केवल specific block को execute करने के लिए उपयोग किए जाने वाले statement को case control statements कहा जाता है।


There are 4 types of case control statements in C language. They are,

1)   switch
2)   break
3)   continue
4)   goto


1)  Switch-case statement

C में switch statement if-else-if ladder statement का एक वैकल्पिक विकल्प है, जो हमें switch variable  नामक single variable के अलग-अलग possibles मानों के लिए कई ऑपरेशन execute करने की अनुमति देता है। यहां, हम एक  single variable के विभिन्न values के लिए कई cases में different statement को परिभाषित कर सकते हैं।

Syntax:

    switch(expression){    
    case value1:    
     //statement;    
     break;  //optional  
    case value2:    
     //statement;    
     break;  //optional  
case value2:    
     //statement;    
     break;  //optional  
    ......    
        
    default:     
   // statement;
If all the cases are not match;    
    }    

Flow chart of switch-case statement:


Rules for switch case statement in C language:
1) Switch expression हमेशा integer या character data type का होना चाहिए
2) Case मे हमेशा character or integer की  value constant होना चाहिए
3) Case की  value को केवल switch statements के अंदर ही  use किया जा सकता है
4) C Switch case मे  break statements use करना जरूरी नही होता है यह optional होता है यदि switch case में कोई statement नहीं मिलता है तो सारे cases case value से मैच होने के बाद भी execute होते है. इसे c language मे c switch case का fall through statement माना जाता है
5) default statement optional होता है। यदि  switch case statement में default statement नहीं है, यह किसी भी समस्या के बिना चलेगा।

1) Example program for switch..case statement in C:


#include <stdio.h>

int main ()
{
   int value = 4;
   switch(value)
   {
     case 1:
     printf(“Value is 1 \n” );
     break;

     case 2:
     printf(“Value is 2 \n” );
     break;

     case 3:
     printf(“Value is 3 \n” );
     break;

     case 4:
     printf(“Value is 4 \n” );
     break;

     default :
     printf(“Value is other than 1,2,3,4 \n” );
   }
   return 0;
}



Output:

Value is 4


2) Example program for Calculator:


#include <stdio.h>
int main()
{
int a,c;
char b;

cout<< "Enter number '2+3' in this formate:";
cin>>a;
cin>>b;
cin>>c;

switch(b)
{
case '+': a=a+c;
cout <<"sum is ="<<a;
  break;
  
case '-': a=a-c;
cout <<"subtraction is ="<<a;
  break;
  
case '*': a=a*c;
cout <<"multiply is ="<<a;
  break;
  
case '/': a=a/c;
cout <<"divide is ="<<a;
  break;
  
case '%': a=a%c;
cout <<"modules is ="<<a;
  break;
  
default:"This is not accepted";
}

return 0;
}



Output:

 Enter number '2+3' in this formate: 5+2
 sum is =7


3) Example Nested switch case statement:



#include<iostream>
using namespace std; 
int main () {  
  
   int a = 1;  
   int b = 2;  
   
   switch(a) {  
     
      case 1:   
         cout<<" value of a evaluated in outer switch:"<<a<<"\n";  
      case 2:  
         switch(b) {  
            case 2:  
cout<<" value of b evaluated in  nested switch:"<<b<<"\n";  
         }  
   }  
   
     cout<<"value is: "<<a<<"\n";
     cout<<"value is: "<<b<<"\n";   
      
   return 0;  




Output:

value of a evaluated in outer switch: 1
value of b evaluated in  nested switch: 2

value is: 1
value is: 2


break, continue, goto के बारे में  part(2) Case control statements में  बता गया है    
   

Comments