Skip to main content

SWITCH CASE EXAMPLE IN C LANGUAGE

How can we simplify the code when more than two if's are required ? 
We use switch statements
Syntax:
  •    switch (expression) 
  •    { 
  •     case constant1:
  •     //codes to be executed if expression equals to constant1;
  •     .
  •     break;
  •     case constant2: 
  •    //codes to be executed if expression equals to constant2; 
  •    . 
  •    break; 
  •    .
  •    default: 
  •    //codes to be executed if expression doesn't match to any cases; 
  • }
Example:
/* Program to create a simple calculator for addition, subtraction, multiplication and division */ 
  1. # include <stdio.h>
  2. # include <conio.h>
  3. int main()
  4. {
  5. char operator; //Declaring variables
  6. float num1,num2; //Declaring variables
  7. printf("Enter operator +, - , * or / :\n"); 
  8. operator=getch();// if operator is other than +, -, * or /, error message is shown 
  9. printf("\nEnter two operands:\n"); 
  10. scanf("%f%f",&num1,&num2); //get input numbers
  11.  switch(operator) // Switches to that particular case
  12. {
  13.  case '+':
  14.  printf("num1+num2=%.2f",num1+num2); // prints calculated value
  15. break; // is used to exit from switch case 
  16.  case '-': 
  17. printf("num1-num2=%.2f",num1-num2); 
  18. break; 
  19. case '*': 
  20. printf("num1*num2=%.2f",num1*num2); 
  21. break; 
  22. case '/': printf("num2/num1=%.2f",num1/num2);
  23.  break;
  24.  default: // If no case is executed then by default this case is executed
  25. printf(Error! operator is not correct"); 
  26. break;
  27.  }
  28.  return 0; 
  29. }

Output :
Enter operator +, -, * or / :
 /
 Enter two operators: 
12
5
num2/num1=2.4;

Description:
This example shows how to use switch case.
Switch can be user when we need two or more if conditions.
Line 1,2 - Here we include two header files.
Line 5,6 - Declares variables.
Line 8 - Gets a character input from console. Important thing is to be noted that if anything except +,-,*,/ is typed  then console returns error.


Actual working of switch case:
Line 11 -  switch(operator): operator is a variable in which we have already taken input.
Now what "switch" does is it switches to particular case character as present in "operator" variable. Suppose operator has value + (plus) then switch() will be switched to first case.
Similarly others work.


Description in marathi: рдорд░ाрдаीрдд рд╡рд░्рдгрди:
рд╣्рдпा рдЙрдзाрд░्рдиाрде рд╕्рд╡ीрдЪ рдХेрд╕ рдХрд╕ा рд╡ाрдкрд░рддाрдд рддे рджिрд▓ेрд▓ा рдЖрд╣े. рд╕्рд╡ीрдЪ рдХेрд╕ рдЖрдкрдг рд╡ाрдкрд░ू рд╢рдХрддो рдЬेрд╡्рд╣ा реи рдкेрдХ्рд╖ा рдЬाрд╕्рдд " рдЗрдл рдПрд▓्рд╕ " рд╡ाрдкрд░ाрдпрдЪे рдЕрд╕ेрд▓ .
рдУрд│ рез,реи - рдЗрдеे рдЖрдкрдг рджोрди рд╣ेрдбрд░ реЮाрдЗрд▓ рд╡ाрдкрд░рдг्рдпा рдХрд░िрддा рддी рдУрд│ рд▓िрд╣िрд▓ेрд▓ी рдЖрд╣े.
рдУрд│ рел,рем - рдк्рд░ोрдЧ्рд░ाрдо рдордз्рдпे рдЬे рд╡ेрд░िрдПрдм्рд▓ рд╡ाрдкрд░рд▓े рдЬाрдгाрд░ рддे рдЗрдеे рд▓िрд╣िрд▓ेрд▓े рдЖрд╣े.
рдУрд│ рео - рдХрди्рд╕ोрд▓ рд╡рд░ूрди рез рдХाрд░ाрдХ्рдЯेрд░ рдШेрдг्рдпाрд╕ाрдаी рд╣ि рдУрд│ рд▓िрд╣िрд▓ेрд▓े рдЖрд╣े.рддो рдУрдк्рд░ेрддрд░  рдиाрд╡ाрдЪा рд╡рд░िрдЕрдм्рд▓ рдордз्рдпे рдЬрдоा рд╣ोрддो.рдЗрдеे "getch" рд╡ाрдкрд░рддांрдиा рдЬрд░ +-*/ рд╣्рдпा рдкेрдХ्рд╖ा рджुрд╕рд░ा рдХाрд╣ी рдХрди्рд╕ोрд▓рд╡рд░ рд▓िрд╣िрд▓े рддрд░ рдХрди्рд╕ोрд▓рд╡рд░ рдПрд░्рд░ोрд░ рджिрд╕ेрд▓.             
 
рд╕्рд╡ीрдЪ  рдХेрд╕ рдк्рд░рдд्рдпрдХ्рд╖ाрдд рдХрд╕ा рдХाрдо рдХрд░рддो ?:
рдУрд│ резрез - Switch(operator) :рдУрдк्рд░ेрддрд░ рд╣ा рдПрдХ рдЕрд╕ा рд╡рд░िрдЕрдм्рд▓ рдЖрд╣े рдЬ्рдпाрдд рдЖрдкрдг рдЖрдзीрдЪ рдпुрд╕рд░ рдХрдбूрди рдХाрд░ाрдХ्рдЯाрд░ рдШेрддрд▓ा рдЖрд╣े.рдЖрддा рдУрдк्рд░ेрддрд░ рд╡рд░िрдЕрдм्рд▓  рдордз्рдпे рдЬे рдХाрд╣ी рдЖрд╣े рддो рдХेрд╕ рдордзрд▓ा рдХोрдб рд░рди рд╣ोрддो. рдЬрд░ рдУрдк्рд░ेрддрд░ рд╡рд░िрдЕрдм्рд▓ рдордз्рдпे ' + ' рдЕрд╕ेрд▓ рддрд░ рдкрд╣िрд▓ा рдХेрд╕ рд░рди рд╣ोрдгाрд░.рдЬрд░ ' - ' рдЕрд╕ेрд▓ рддрд░ рджुрд╕рд░ा.   

Comments

Popular posts from this blog

IOT : GARDEN WATER SPRINKLER SYSTEM

IoT projects have gained immense popularity due to their ability to leverage the power of interconnected devices and data to create innovative solutions across various domains. IoT projects involve integrating sensors, actuators, and communication technologies to enable the seamless exchange of data between physical devices and the internet. These projects range from simple DIY experiments to complex industrial applications, each offering unique opportunities to transform our lives and improve efficiency. Whether it's creating a smart home automation system, monitoring environmental conditions, optimizing energy usage, or developing intelligent transportation systems, applications of IoT open up a world of possibilities for innovation and connectivity. By harnessing the potential of IoT, we can build intelligent and interconnected systems that revolutionize industries, enhance daily life, and pave the way for a more efficient and sustainable future. In this project we will be using...

POWER BI - THE ARCHITECTURE

Power BI Architecture: A Comprehensive Guide to Building Effective Data Analytics Solutions     Understanding Power BI Architecture: Power BI's architecture comprises four main components, each playing a pivotal role in the data analytics process: Data Sources:   Power BI connects to a wide range of data sources, including databases, cloud services, Excel files, web services, and more. This versatility enables organizations to consolidate their data from various systems into a single dashboard. Data Transformation:   Once the data is sourced, it undergoes transformation and cleaning processes to make it suitable for analysis. Power BI's Power Query Editor provides a user-friendly interface to manipulate, filter, and shape data according to specific requirements. Data Model:   The heart of Power BI's architecture lies in its data model, whic...

TOUCH PLATE BASED DOOR BELL

Title:  Touch plate based door bell  Circuit:  Components: IC 555 Resistors: 1 M, 100k, 330 ohms Transistor: BC547  PN2222A Capacitor: 10n 1 Copper plate : as touch plate. A 6v battery An LED / Ic UM66 Description: This is the simple circuit for touch plate based security system. In this project what basically done is, circuit detects stray voltages produced by mains voltage and electrostatic built  up in the room.If sufficient static voltage is detected by the plate then chip will charge up. Transistor BC 547 or PN2222A is used basically to increase the sensitivity.In place of led just connect IC um 66(melody IC). Applications: In homes, of course. This can be specially used in places like hospitals, when patients need to call doctor by himself.