‘switch’ statement in C

‘switch’ statement in C

Let us consider a situation in which we have more than one options out of which we need to select/recognise only one option that is to be executed. Such kind of problems can be answered using nested if statement.

 But as the number of options increases, the complexity of the program also gets increased and program become lengthy . This type of problem can be solved very easily by using switch statement. Using switch statement, one can select only one option from more number of options very easily.

 In switch statement, we consider a value that is to be compared with a value associated with every option. Whenever the given value is same/similar with the value associated with an option, the execution of program starts from that option. In switch statement every option is defined as a ‘case’.

Syntax  of switch statements along with the execution of flow chart diagram…

   Ex:   SYNTAX:-

            switch (expression or value)

     {

            case value 1: set of statements;

                        …………

            case value 2 : set of statements;

                        ………..

           case value3 : set of statements;

                        ……….

           case value4: set of statements;

            ……..

            ………

            Default : set of statements;

           }

The switch statement contains one or more number of cases and each case has a value associated with it. At first switch statement compares the first case value with the switchValue, if it gets matched the execution starts from the first case. If it doesn’t match the switch statement compares the second case value with the switchValue and if it is matched the execution starts from the second case. This process continues until it finds a match. If no case value matches with the switchValue specified in the switch statement, then a special case called default is executed.

When a case value matches with the switchValue, the execution starts from that particular case. This execution flow continues with next case statements also. To avoid this, we use “break” statement at the end of each case. That means the break statement is used to terminate the switch statement. However it is optional.

Example Program | Display pressed digit in words.

#include<stdio.h>

#include<conio.h>

void main(){

   int n ;

   clrscr() ;

   printf(“Enter any digit: “) ;

   scanf(“%d”, &n) ;

   switch( n )

   {

        case 0: printf(“ZERO”) ;

               break ;

        case 1: printf(“ONE”) ;

               break ;

        case 2: printf(“TWO”) ;

               break ;

        case 3: printf(“THREE”) ;

               break ;

        case 4: printf(“FOUR”) ;

               break ;

        case 5: printf(“FIVE”) ;

               break ;

        case 6: printf(“SIX”) ;

               break ;

        case 7: printf(“SEVEN”) ;

               break ;

        case 8: printf(“EIGHT”) ;

               break ;

        case 9: printf(“NINE”) ;

               break ;

        default: printf(“Not a Digit”) ;

   }

   getch() ;

}

Output 1:

            Enter any digit :5

            FIVE

Output 2:

            Enter any digit :90

            Not a digit

MOST IMPORTANT POINTS TO BE REMEMBERED

When we use switch statement, we must follow the following…

  • Both switch and case are keywords so they must be used only in lower case letters.
  • The data type of case value and the value specified in switch statement must be same.
  • switch and case values must be either integer or character but not float or string.
  • A switch statement can contain any number of cases.
  • The keyword case and its value must be superated with a white space.
  • The case values need not be defined in sequence, they can be in any order.
  • The default case is optional and it can be defined anywhere inside the switch statement.
  • The switch value might be a direct value, a variable or an expression.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contribute
close slider
present

Contribute and Win Exciting Prizes & Gifts



whatsapp

You can also send file via Whatsapp

+91 9155653926

If you have any year ,any subject,any semester question paper and other study materials please send us

अगर आपके पास किसी सेमेस्टर के किसी भी साल और किसी भी विषय का क्वेश्चन पेपर है और कोई अन्य पढाई सामग्री तो उसे हमलोगों को भेजे