Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what is the use of switch in language c and how to use it

–1 vote
asked Jan 26, 2020 by anonymous

4 Answers

0 votes
answered Jan 27, 2020 by anonymous

Switch is a Control Flow statement. It is use to evaluate expressions. Look at this code snippet:

char grade = 'A';

   switch(grade) {
      case 'A' :
         printf("Excellent!\n" );
         break;
      case 'B' :
      case 'C' :
         printf("Well done\n" );
         break;
      default :
         printf("Invalid grade\n" );
   }
Above statement evaluates the value of the variable grade. The switch statement look each case then print the corresponding statement.
0 votes
answered Jan 27, 2020 by harish1117 (140 points)
switch is mainly used in programs where the multiple values to be compared and executed is to be done in random
+1 vote
answered Jan 28, 2020 by anonymous
//switch statements are equivalent to lots of if-elseif-else statements

#include <stdio.h>

void main(){

int a = 2;

switch(a){      //variable or expression to investigate
    case 1:     //characters go within ' '  e.g. case 'a'
        printf("A = 1");
        break;    //In C, need break else will carry on and print all statements until next break or will print default if no breaks
    case 2:
        printf("A = 2");
        break;
    case 3:
        continue;
    default:    // need default and statements etc else switch doesn't work
        printf("Value of A not found in this switch");
}
}

//Another key variable is continue. This means if switch matches, will move on and keep going through switches until another match or default
//In this switch, will print case 2 statements since condition matches (a == 2)
0 votes
answered Jan 31, 2020 by teja sree (360 points)
The switch statement is a multiway branch statement.It provides an easy way to dispatch execution to different parts of code based on the value of expression.

void main()

{

int roll=3;

switch(roll)

{

  case 1:

            printf("I am Sree");

             break;

case 2:

        printf("I am Teja");

        break;

case 3:

     printf("I am priya");

    break;

default:

   printf("No student found");

   break;

}

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...