C Programming

Switch Control in C

In the C programming language, switch control offers us options for menu-driven programs. Actually, with the help of a menu if – a statement, we can also build a menu-driven program. But it may increase the complexity of our program. Execution time is also increasing. To resolve the problem, switch control is introduced in C.

When we get more than one option, then nesting (if-else) is not readable to the point of view of our program, and then we use switch control.

Syntax

    switch ( expression )
    {
        case constant: code ;
        case constant: code ;
        case constant: code ;
default : code ;
    }

In expression, the true or false condition is not gettable. The result is to be taken. A Switch means to jump. As the result of the expression, we will get the constant, and the execution will go that case no. If this constant does not match, it goes to the default case. It runs only one time. Constant may be any int or char in type. But float or any real no is not possible.

If we want to run only onecase, we have to write a break in every case. Then control exits the body of the switch. It means that the case will be run, only that expression tells.

It is not necessary; a constant is a group of sequence no. It may be random no 5, -4, 143……

Default case, we can write anywhere in the switch body.

Programming Example 1

#include<stdio.h>

int main()
{
    int  choice, a, b ;
    float  s ;
while(1)        //if we want to see the menu-driven program again & again then we write while( 1 ).It is a true condition. //
    {
printf ( "\n1. Addition " ) ;
        printf ( "\n2. Subtraction " ) ;
        printf ("\n3. Multiplication " ) ;
        printf ( "\n4. Division " ) ;
        printf ( "\n5. Exit " ) ;
        printf ( "\n enter your choice : " ) ;
        scanf ( " %d ", & choice ) ;
        switch ( choice )
        {
        case 1 :
            printf ( " enter two numbers " ) ;
            scanf ( "%d%d ", & a, & b ) ;
            printf ( " sum of %d and %d is %d ", a, b, a+b ) ;
break;
        case 2 :
            printf ( " enter two numbers " ) ;
            scanf ( "%d%d", & a, & b ) ;
            printf ( " subtraction of the given two numbers is %d ",  a-b ) ;
break ;
        case 3 :
            printf ( " enter two numbers " ) ;
            scanf ( " %d%d ", & a, & b);
            printf ( " multiplication of the given two numbers is %d ", a*b ) ;
break ;
        case 4 :
            printf ( " enter two numbers " ) ;
            scanf ( "%f%d ", & s, & a ) ;
            printf ( " division of the two numbers is %f ", s/a ) ;
break ;
        case 5 :
                         exit(0);   // ‘ 0 ‘ mean normal termination of  the program ;
default :
            printf ( " invalid choice " ) ;
        }
        getch () ;
    }
    return 0 ;
}

Output

Explanation

When we wrote any non-zero no inside the parenthesis of while its mean, it evaluates the true condition. So, this condition will never be false. If we want to end this program, we have to write getch() inside the while loop. If the user wants to exit from the menu-driven program, we have to write another case called exit (0).

Programming Example 2

#include<stdio.h>

#include<stdlib.h>

int main()
{
    int  choice, a, count = 0 ;
while( 1 )
    {
        int  r = 0, rev = 0 ;
printf ( " \n1. prime number " ) ;
printf ( "\n2. odd even " ) ;
printf ( " \n3. printing N numbers " ) ;
printf ( " \n4. reverse of a number " ) ;
printf ( " \n5. exit " ) ;
printf ( " \n enter your choice : " ) ;
scanf ( " %d ", & choice ) ;
        switch ( choice )
        {
        case 1 :
printf ( " enter a number\t " ) ;
scanf ( " %d", & a ) ;
for ( int i = 1; i <= a ; i++ )
            {
                if ( a % i == 0 )
                {
                    count++ ;
                }
            }
            if ( count == 2)
            {
             printf ( " %d is a prime number ", a ) ;
            }
            else
            {
          printf ( " %d is not a prime number ", a ) ;
            }
            break ;
        case 2 :
printf ( " enter a number\t " ) ;
scanf ( " %d", & a ) ;
            if  (a % 2 == 0 )
printf ( " the number is even " ) ;
            else
printf ( " the number is odd " ) ;
                break ;
        case 3 :
printf ( " enter a number\t " ) ;
scanf ( " %d", & a ) ;
printf ( " natural numbers from 1 to %d :\n ", a) ;
for ( int s = 1 ; s <= a; s++)
            {
                printf  (" %d\n ", s ) ;
            }
            break ;
        case 4 :
printf ( " enter a number\t " ) ;
scanf ( "%d", & a) ;
            while ( a !=0 )
            {
                r = a % 10 ;
                rev = rev * 10 + r ;
                a / = 10 ;
            }
printf( " reverse of the number is %d ", rev ) ;
            break ;
        case 5 :
exit ( 0 ) ;
        default :
printf ( " invalid choice " ) ;
        }
    }
}

Output

Explanation

Programming Example 2 is another example of switch control expression. Here 5 cases are present according to the requirement of the program. Each case holds an option for the user to make his / her choice to run the program. As I can see from the output, the user can choose to run his program repeatedly until he or she exits the program by choosing case 5. It is called a menu-driven program.

Conclusion

By using a switch case, the user can get options according to his or her requirements. By doing this, the C language gives facilities to the user and the coder to make the program more convenient and easier. It also reduces the space complexity and time complexity of the program. As a result, programs became more robust mean error-free.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com