Switch Case in C





  • The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch-case-default.

  • The Switch executes the case where a match is found and then all subsequent cases till end, so stop that fall through we use break; statement in each case.

  • A switch works with the integral or enumerated types.

Syntax :

switch(expression){
   case value1  :
      statement(s);
      break; //optional
   case value2  :
      statement(s);
      break; //optional
    .....
/* you can have any number of case statements. */
   default : //Optional
      statement(s);
  /* code to be executed if all cases are not matched */
}               

Block Diagram :

prowessapps.in

Example:

WAP to give choice to user from menu to perform action.
#include<stdio.h>
int main(){
  int x ;
  printf("MENU\n----------");
  printf("\n1. for Hello");
  printf("\n2. for Bye");
  printf("\n-----------");
  printf("\nEnter Your Choice: ");
  scanf("%d",&x);
 switch(x) {
  case 1: 
        printf("HELLO USER!!");
 	break;
  case 2:
        printf("BYE USER!!");
	break;
  default: 
        printf("INVALID!!");
  }//switch closed
 }              
OUTPUT:
MENU
-----------
1. for Hello
2. for Bye
-----------
Enter Your Choice: 1
HELLO USER!!

Next topic is loops






 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in



CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved