Showing posts with label Conditional Statements. Show all posts
Showing posts with label Conditional Statements. Show all posts

switch statement

Switch Statement
switch statement एक multiple branch selection statement है ।
यह Selection statement successively, integer और character constant के सापेक्ष expression की value को test करता है। यदि match मिलता है तो जो statement उससे जूड़े होते है वो execute हो जाते है। 
Syntax 


switch
switch statement में जो expression होता है वो पहले solve होता है और expression की value प्रत्येक case से match करता है यदि match मिलता है तो जितने भी statement उस case में होते है वो सभी execute होते है जब तक कि break नही मिलता है।  यदि case में break नही मिलता तो उसके बाद जितने भी statement होते है execute हो जाते है जब तक कि break या switch statement का end नही मिलता इस condition को fall through कहते है और default statement तब execute होता है जब किसी भी case से match नही मिलता।


/* Program to translate to its equivalent name of the month (1 - January 2 -February------12 - December) */

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int month;   
               clrscr();   
               printf("Enter number between 1 to 12:");  
               scanf(%d",&month); 
               switch(month)  
               {     
                    case 1:     
                    printf(" January");     
                    break;      
                    case 2:    
                    printf(" February");     
                    break;     
                    case 3:    
                    printf(" March");    
                    break;      
                    case 4:    
                    printf(" April");    
                    break;    
                    case 5:      
                    printf(" May");    
                    break;     
                    case 6:    
                    printf(" June");   
                    break;    
                    case 7:      
                    printf("July");      
                    break;    
                    case 8:     
                    printf(" August");    
                    break;      
                    case 9:      
                    printf(" September");    
                    break;     
                    case 10:     
                    printf(" October");   
                    break;    
                    case 11:    
                    printf(" November");   
                    break;     
                    case 12:   
                    printf(" December");     
                    break;      
                    default:    
                    printf(" Invalid Input");   
               }   
                   getch(); 
                   return 0;
             }
Output :
Enter number between 1 to 12: 3
March
दोबारा रन कराने पर
Enter number between 1 to 12: 14
Invalid Input


Use of multiple case in switch statement

एक उदाहरण के द्वारा समझते है।

/* Program to check vowels */

             #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               char ch;   
               clrscr();   
               printf("Enter a character:");  
               scanf(%c",&ch); 
               switch(ch)  
               {     
                    case a:     
                    case e:
                    case i:
                    case o;
                    case u:
                    printf(" Vowel");     
                    break;      
                    default:    
                    printf(" Not a vowel");   
               }   
                   getch(); 
                   return 0;
             }
Output :
Enter a character : a
vowel
दोबारा रन कराने पर
Enter a character : p
Not a Vowel



NEXT   :  goto statement
PREVIOUS : if - else if ladder

if - else if ladder

if -else-if Ladder 
if -else-if Ladder एक Multi-way decision statement है।

Syntax :
if else if ladder

if-else-if ladder में हम एक के बाद एक Condition/Expression दे सकते है जैसाकि उपरोक्त Syntax में बताया गया है। इसमें Condition ऊपर से नीचे के क्रम Test होती है और जो भी Condition, True होती है तो  जो भी Statement उससे जुड़े होते है वो Execute हो जाते है और बाकी सभी छोड़ दिये जाते है और यदि कोई भी Statement, True नही होता है तो else से जुड़े Statement, Execute होते है। 
Example -1

*/ Program to input number of week's day and translate to its equivalent name of the day */

                 #include<stdio.h>

                #include<conio.h>

                int main(void)

               {
                     int number;
                     clrscr();
                     printf("Enter a number(1-7)");
                     scanf("%d",&number);
                     if (number == 1)
                    {
                          printf("Monday\n");
                     }
                    else if (number == 2)
                   {
                         printf("Tuesday\n");
                   }
                    else if (number == 3)
                   {
                         printf("Wednesday\n");
                   }
                   else if (number == 4)
                   {
                         printf("Thursday\n");
                   }
                   else if (number == 5)
                   {
                         printf("Friday\n");
                   }
                  else if (number == 6)
                  {
                         printf("Saturday\n");
                  }
                  else if (number == 7)
                  {
                        printf("Sunday\n");
                  }
                 else
                 {
                        printf("Invalid Input\n");
                  }
                  getch();
                  return 0;
             }

Output :
Enter a number (1-7) 2
Tuesday 
दोबारा रन कराने पर
Enter a number (1-7) 8
Invalid Input


उपरोक्त प्रोग्राम में सबसे पहले एक मैसेज आता है कि "Enter a number (1-7)" और हम एक नबम्बर Input करा देते है जैसे मानलीजिए 2 तो number==2, condition, True हो जाती है  और हमे Result मिलता है Tuesday और यदि दोबारा रन कराते है और फिर से मैसेज आता है कि "Enter a number (1-7)" और हम एक नबम्बर Input करा देते है जैसे मानलीजिए 8 तो कोई भी condition, True नही होती है और else वाला Statement Execute होता है और Result मिलता है Invalid Input.  

Example -2

*/ Program to find roots of quadratic equation */

                 #include<stdio.h>

                 #include<conio.h>
                 #include<math.h>                
                 int main(void)
               {
                     float a,b,c,x1,x2,d;
                     clrscr();
                     printf("Enter value of a,b & c");
                     scanf("%f%f%f",&a,&b,&c);
                     d= b*b-4*a*c;

                     if (d>0)

                    {
                      x1= (-b+sqrt(d))/(2*a);
                      x2= (-b+sqrt(d))/(2*a);                                      
                      printf("Roots are distinct x1 =%f & x2=%f", x1,x2);
                     }
                    else if (d == 0)
                    {
                         x1=x2= -b /(2*a);
                         printf("Roots are equal x1 =%f & x2=%f", x1,x2);
                    }
                   else
                   {
                        x1= -b /(2*a);
                        x2 = sqrt(-d)/(2*a);
                     printf("Roots are imaginary x1 = %f & x2=%f", x1,x2);
        }
                     getch();
                     return 0;
             }

Output :

दोबारा रन कराने पर



NEXT   :  switch statement
PREVIOUS : Nested if -else

Nested if else

Nested if else
Nested if else  का मतलब if के अंदर if -else और else के अंदर  भी if -else.
Syntax
nested if else

/* Divisibility Test */

         #include<stdio.h>

         #include<conio.h>

       int main(void)

      {
          int number;
         clrscr();
         printf("Enter the number for divisibility test ");
         scanf("%d",&number);
         if (number %2 ==0)
         {
                if(number %3 ==0)
               {
                  printf("Number divisible by 2 & 3");
               }
               else
              {
                printf("Number divisible by 2 only ");
              }
         }
         else
        {
               if(number %3 ==0)
              {
                  printf("Number divisible by 3 only ");
              }
             else
             {
                 printf("Not divisible by 2 & 3");
             }
       }
             getch();
             return 0;
  }

उपरोक्त में if block के अंदर if -else और else block के अंदर if -else का उपयोग किया गया है । इसमें सबसे पहले आपके पास एक मैसेज आयेगा  Enter the number for divisibility test और हम एक नम्बर इनपुट करा देते है उसके बाद प्रोग्राम का कन्ट्रोल if (number %2 ==0) और condition, test करेगा यदि  condition सही होती है तो उसके अंदर वाले if(number %3 ==0) पर आयेगा अगर यह भी condition, true होती है तो output आयेगा  Number divisible by 2 & 3 और यदि if(number %3 ==0) यह condition, false होती है  तो output आयेगा  Number divisible by 2 only । यदि if (number %2 ==0) condition, false होती है तो इस block के if और else दोनो execute नही होगे ।    




NEXT   :   if - else if ladder
PREVIOUS : if -else statement -II

if -else statement -II

कुछ और उदाहरण 
*/ Program to check whether the number is Even or Odd */

                 #include<stdio.h>

                #include<conio.h>

                int main(void)

               {
                     int number;
                     clrscr();
                     printf("Enter a number");
                     scanf("%d",&number);
                     if (number %2 ==0)
                    {
                          printf("Even Number");
                     }
                    else
                   {
                        printf("Odd Number");
                   }
                  getch();
                 return 0;
             }

Output :
Enter a number 10
Even Number
दोबारा रन कराने पर
Enter a number 3
Odd Number


Use of Logical Operator 


*/ Program to check whether the year leap year or not */

                 #include<stdio.h>

                #include<conio.h>

                int main(void)

               {
                     int year;
                     clrscr();
                     printf("Enter the year");
                     scanf("%d",&year);
                     if (year %4 ==0&& year %100!=0) || (year%400==0))
                    {
                          printf("Leap Year");
                     }
                    else
                   {
                        printf("Not a Leap Year");
                   }
                  getch();
                 return 0;
             }

Output :
Enter the year 
Leap Year
दोबारा रन कराने पर
Enter the year
Not a Leap year

Program 
/* Divisibility Test */

                #include<stdio.h>

                #include<conio.h>

                int main(void)

               {
                   int number;
                   clrscr();
                   printf("Enter the number for divisibility test ");
                   scanf("%d",&number);
                     if (number %2 ==0&& number %3==0)
                    {
                          printf("Number divisible by 2 & 3");
                     }
                    else
                   {
                          printf("Not divisible by 2 & 3");
                   }
                  getch();
                 return 0;
             }


Output : 
Enter the number for divisibility test 18
Number divisible by 2 & 3
दोबारा रन कराने पर
Enter the number for divisibility test 10
Not divisible by 2 & 3




NEXT   :  Nested if -else
PREVIOUS : if - else statement 

if else statement

if else statement

Syntax

a


if statement, Condition/Expression को Test करता है यदि Condition/ Expression, True ( Non Zero) होती है तो जो भी Statements, if Block में होते है वो सभी Execute जाते है और यदि Condition/ Expression, False होती है तो जो भी Statements, else Block में होते है सभी Execute होते है।

Note : यदि if Statement में Single Statement है तो हम Curly Braces को छोड़ भी सकते है। 

Example : 


 if(10>8)

 {
       printf("10 is greater than 8 ");
  }
else
 {
      printf("10 is not greater than 8");
 }

अब प्रोग्राम के द्वारा समझते है


*/ Program to check whether the number is equal to 10 or not */

            #include<stdio.h>

            #include<conio.h>

            int main(void)

           {
               int number;
               clrscr();
               printf("Enter a number");
               scanf("%d",&number);
               if(number==10) //  condition
               {
                        printf("Right");
                }
               else
              {
                       printf("Wrong");
               }
                      getch();
                      return 0;
           }

Output :
Enter a number 10
Right

दोबारा रन कराने पर


Enter a number 20

Wrong

उपरोक्त प्रोग्राम को जब रन कराते है तो सबसे पहले एक मैसेज आता है कि Enter a number और हम कोई एक Number इनपुट करा देते है । if statement इस Number को चैक करता है कि इनपुट किया गया number, 10 के बराबर है कि नही । अगर इनपुट Number 10 के बराबर होता है तो if statement की Condition, True हो जाती है और जो भी Statement if block के अंदर लिखे होते वो सभी Execute हो जाते है और यदि इनपुट किया गया Number 10 के बराबर नही होता है तो else Statement, Execute होता है जो भी Statement else block के अंदर लिखे होते वो सभी Execute हो जाते है
जैसा कि उपरोक्त Output में है इनमें सबसे पहले एक मैसेज आता है कि Enter a number और हम एक Number  10 इनपुट करा देते है चूकिः इनपुट किया गया Number 10 के बराबर है और if statement की Condition, True (Non-Zero) होती है।  और हमे Result मिलता है Right । यदि हम उपरोक्त प्रोग्राम को दोबारा रन कराते है तो Output में एक मैसेज आता है कि Enter a number और हम एक Number  20 इनपुट करा देते है चूकिः इनपुट किया गया Number 10 के बराबर नही है और if statement की Condition, False (Zero) होती है  और इस case में else statement, Execute होता है और हमे Output मिलता है । Wrong

Use of = Assignment Operator & == Equal to Operator 

हम देखते है कि कभी कभी हमारा प्रोग्राम सही होने के बावजूद हमारा आउटपुट सही नही आता है  और न प्रोग्राम में कोई Error आती है।
उदाहरण के लिए उपरोक्त प्रोग्राम को दोबारा देखते है।


*/ Program to check whether the number is equal to 10 or not */

                #include<stdio.h>

                #include<conio.h>

               int main(void)

              {
                   int number;
                   clrscr();
                   printf("Enter a number");
                   scanf("%d",&number);
                   if(number=10) //  condition
                  {
                           printf("Right");
                  }
                  else
                 {
                          printf("Wrong");
                  }
                           getch();
                           return 0;
                }

Output:

Enter a number 10
Right

दोबारा रन कराने पर


Enter a number 20

Right

उपरोक्त प्रोग्राम हमने Equal to, operator की जगह Assignment operator का Use किया है । जो इस तरह से है 
number =10 इसका मतलब यह है कि हम number में 10 को Assign कर रहे है । चूकिः 10 एक non zero value है तो इस case में हमारी condition हमेशा true रहेगी । 
  Example of relational operator 


*/ Program to check the validity of a Triangle*/

                #include<stdio.h>

                #include<conio.h>

               int main(void)

              {
                   float a,b,c;
                   clrscr();
                   printf("Enter the sides:");
                   scanf("%f%f%f",&a,&b,&c);
                   if(a+b > c && b+c > c && a+c > b) //  condition
                  {
                           printf("Triangle is valid");
                  }
                  else
                 {
                          printf("Triangle is not valid");
                  }
                           getch();
                           return 0;
                }

Output:

Enter the sides: 


दोबारा रन कराने पर


Enter the sides 








NEXT   :  if -else statement -II
PREVIOUS : if statement