Wednesday 15 October 2014

Control Statement Switch

Switch
-------------------------------------------------------------------------------------------------------------------
This is a multi-directional control statement. sometime there is need in program choice among number of alternatives. if we want to create a program that inclde number of option but we can choose among of this, for that we use switch condition.

Syntax:- 
             switch (expression)
                   {
                      case 1:
                      statement;
                      ...............;
                      
                      case 2:
                      statement;
                      ...............;

                      case n:
                      statement;
                      ...............;

                      case default:
                      statement;
                      ...............;

Here switch, case and default are keywords, The expression in the case which is integer like 1 , 2 ..n, it can be integer character variable. switch expression that is enter into switch matches to case expression then executes any one case, if expression is not find into case then default executed and print massage user define. the case keyword should be of integer or character type. They can be either constants or constant expressions. These constant must be different from one another, we can't use float.

Method to writing valid and invalid ways of writing switch and case.

Valid
   
 int a,b,c   char d,e     float f;
switch(a), switch(a>b), switch(func(a,b))

case 4:       case 'a':       case 2+4:       case 'a'>'b':

Invalid 

  int a,b,c   char d,e     float f;

switch(f)  switch(a+3.5)

case "second"              case 3.5:              case a+2:


Working of switch

Firstly the switch expression is evaluated, then the value of this expression is compared one by one with every case constant.If the value of expression matches with any case constant, then all statements under that particular case are executed. if none of the case constant matches with the value of the expression then the block of statements under default is executed."default" is optional, if it is not present and no case matches then no action takes place.

// program to understand the switch control statement.

#include<stdio.h>
main()
{
    int choice;
    printf("Enter Your choice:");
    scanf("%d",&choice);
    switch(choice)
    {
        case 1:
        printf("First\n");
        
          case 2:
          printf("Second\n");

           case 3:
           printf("Third\n");

           default:
           printf("Wrong choice:\n");

}


Output

Enter your choice : 2
second 
Third 
Wrong choice

Here value of choice matches with second case so all the statement after case 2 are executed sequentially. because its flow is sequentially and when case 2 is find but after this there is no stop or break statement that stop the flow of switch so it print all case after selected case. we use break keyword in every case.
        
        
#include<stdio.h>
main()
{
    int choice;
    printf("Enter Your choice:");
    scanf("%d",&choice);
    switch(choice)
    {
        case 1:
        printf("First\n");
        break;
          
          case 2:
          printf("Second\n");
           break;
          
           case 3:
           printf("Third\n");
             break;
          
           default:
           printf("Wrong choice:\n");

}

Output

Enter your choice : 2
second 

in this example we use break keyword because sequential flow in the switch when our input matches with any case then that case execute after that when compiler find break keyword, compiler stop and go out of switch.

Flow Chart of Switch



switch executed when switch expression==case constant, otherwise default is executed.



// program to perform arithmetic calculations on           integer.

#include<stdio.h>

void main()
{
char op;
int a,b;
printf("Enter number operator and arithmetic number");
scanf("%d%c%d",&a,&op,&b);
switch(op)
{
case '+':
printf("Result=%d\n",a+b);
break;

case '-'
printf("Result=%d\n",a-b);
break;

case '*'
printf("Result=%d\n",a*b);
break;

case '/'
printf("Result=%d\n",a/b);
break;

case '%'
printf("Result=%d\n",a%b);
break;

default:
printf("Enter Valid operator\n");
break;

} // end of switch
} // end of main

Output :

Enter number operator and another number : 2+5

Result :7

in this program, the valid operator for multiplication is only '*', if want to make 'x' 'X' also valid operator for multiplication.


                 

                      

No comments:

Post a Comment