Switch statement in C language

 

Switch statement In C language

Hello! After this tutorial you will be able to understand what is switch statement and how to use switch in c language.

In simple word we can say that switch provide us option or alternatives and flexibility in a code.as we use if…else ladder but it is too complicated and we can make mistake during if..else ladder.

For this we use switch statement,however the syntax of switch statement is much easier to read and write.

Syntax:-

switch (expression)

{

    case constant a:

      // statements

      break;

 

    case constant b:

      // statements

      break;

    default:

      // default statements

}

How this switch statement work? 

The switch expression is evaluated once and compared with the value of each case label.

1.if there is a match of condition the corresponding statement after the matching label are executed.  for example if a value of expression label are equal to constant b, statements after case constant b: are executed until break will be encountered.

2.if there is no match, the default  statements are executed.

3.if we do not use break , all statements after the matching label are executed.

4.if we want to use default we can use but it is not mandatory.

Here I am sharing a C code using switch so you can understand switch better.

This is a code made by me for calculator using switch

#include<stdio.h>

#include<conio.h>

main()

{

            float a, b;

            int choice;

            printf (" calculator design by smart engineer \n");

            printf ("enter the value of two operands:");

            scanf("%f, %f", &a, &b);

            printf("press 1 for addition:\n");

            printf("press 2 for substration:\n");

            printf("press 3 for multiply:\n");

            printf("press 4 for division:\n");

            printf("enter the choice:");

            scanf("%d", &choice);

           

            switch(choice)

            {

                        case 1:{

                                    float sum=a+b;

                                    printf("sum is: %f",  sum);

                                    break;

                        }

                        case 2:{

                                    float subs=a-b;

                                    printf("subs is: %f", subs);

                                    break;

                        }

                        case 3:{

                                    float mult=a*b;

                                    printf("mult is: %f", mult);

                                    break;

                        }

                        case 4:{

                                    float div=a/b;

                                    printf("div is: %f",div);

                                    break;

                        }

                        default:{

                                    break;

                        }          

            }          

            return 0;

            getch ();

}

 

 

Rule of applying switch statements

1.we can have many number of case statements within switch. each case is followed by value to be a compared to end a colon.

2.when a variable match the on equal to a case, the statements following that case will executed until a break statement is reached.

3.when a break statements is reached, the switch terminates, and the flow of control jumps to the next line following switch statements.

4.break statements is not necessary in every case.it is optional.

 

 

MCQs based on switch case


1. Consider the following program.

  switch(input)

{

    case '1':

        printf("One");

    case '2':

        printf("Two");

    case '3':

        printf(""Three");

    default:

        Printf("Default");

        break;

}

 

What will be printed when input is 2?

a) Two Three default

b) Two

c) Two default

d) Two Two

answer-a

2.The advantage of ‘switch’ statement over ‘if’ is that it leads to more structured program.

a) True

b) False

answer- a

3.Consider the following C program. What is the Value of a?

#include<stdio.h>

int main()

{

    int a=7, b=5;

    switch(a = a % b)

    {

        case 1:

            a = a - b;

        case 2:

            a = a + b;

        case 3:

            a = a * b;

        case 4:

            a = a / b;

        default:

            a = a;

    }

return 0;

}

a) 7

b) 5

c) 9

d) None

answer-a

 

4. What will be the output of the following C code? (Assume that we have entered the value 1 in input)

#include <stdio.h>

void main()

    {

        char *ch;

        printf("enter a value between 1 to 3:");

        scanf("%s", ch);

        switch (ch)

        {

           case "1":

              printf("1");

              break;

           case "2":

              printf("2");

              break;

        }

    }

a) 1

b) Compile time error

c) 2

d) error

answer-b

5. What will be the output of the following C code?

 #include <stdio.h>

int main()

    {

        int a = 1, b = 1;

        switch (a)

        {

           case a*b:

              printf("yes ");

           case a-b:

              printf("no\n");

              break;

        }

    }

a) yes

b) no

c) Compile time error

d) yes no

answer-c


In the next blog we will discuss go to statement and other decision making component.

thankyou!


 

Comments

Popular posts from this blog

Decision making with if statement and else..if

Call by value and by reference in function