Switch-statement .

In this session i have discussed switch statement in C .


Switch-Statement :

Switch is also a decision making and branching statemnet . It used to take the single selection from the multiple choice .

Switch-statement-syntax :

CSharp Switch image

Example : Write a program check the days of week according to given number .

    #includ<stdio.h> 
    int main()
    {  
            int i=4;
            switch(i)
            {
                case 1:
                {
                    printf("Sunday");
                    break;
                }
                case 2:
                {
                    printf("Monday");
                    break;
                }
                case 3:
                {
                    printf("Tuesday");
                    break;
                }
                case 4:
                {
                    printf("Wednesday");
                    break;
                }
                case 5:
                {
                    printf("Thursday");
                    break;
                }
                case 6:
                {
                    printf("Friday");
                    break;
                }
                case 7:
                {
                    printf("Saturday");
                    break;
                }
                default :
                {
                    printf("Invalid number");
                    break;
                }
            
            }
            return 0;
    }
    
    OUTPUT : Wednesday
    


Example : Write a program that perform 1-Addition , 2-Subtraction , 3-multiplication , 4 Division .

    #includ<stdio.h> 
    int main()
    {
            int op=3;
            int x=20;
            int y=10;
            switch(op)
            {
                case 1:
                {
                    int sum=x+y;
                    printf("Sum= %d",sum);
                    break;
                }
                case 2:
                {
                    int sub=x-y;
                    printf("Sub= %d",sub);
                    break;
                }
                case 3:
                {
                    int multi=x*y;
                    printf("Multi= %d",multi);
                    break;
                }
                case 4:
                {
                    int div=x/y;
                    printf("Div= %d",div);
                    break;
                }
                default :
                {
                    printf("Invalid Number");
                    break;
                }
            }
            return 0;
    }
    
    OUTPUT : Multi= 200
    


Welcome to AndroidATPoint.

back image                     next image