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

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ A load cell is a transducer that converts force (weight) into an electrical signal. The HX711 is a precision 24-bit analog-to-digital converter (ADC) designed for weigh scales. Today we’ll connect a load cell to Raspberry Pi 3 using the HX711 module. ๐Ÿงช ๐Ÿ”ง Components Required Component Quantity Raspberry Pi 3 1 Load Cell 1 HX711 Module 1 Jumper Wires 6 Breadboard (optional) 1 ๐Ÿ”Œ Pin Connections HX711 Pin Raspberry Pi Pin Pin Number VCC 5V Pin 2 GND Ground Pin 6 DT GPIO 5 Pin 29 SCK GPIO 6 Pin 31 Figure: Load Cell connected to Raspberry Pi 3 via HX711 ๐Ÿ’ป Python Code from hx711 import HX711 import RPi.GPIO as GPIO import time hx = HX711(dout_pin=5, pd_sck_pin=6) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(1) hx.reset()...

Interfacing Sound Sensor with Raspberry Pi 3

๐Ÿ”น Overview The KY-037 is a high-sensitivity sound detection sensor that can detect noise levels in the environment. It provides both analog and digital outputs. In this tutorial, we’ll interface the digital output of KY-037 with Raspberry Pi 3 Model B+ (without using an ADC like MCP3008) and detect sound events.

Interfacing Water Flow Sensor with Raspberry Pi 3 ๐Ÿšฟ

Interfacing Water Flow Sensor with Raspberry Pi 3 ๐Ÿšฟ ๐ŸŽฏ Objective To measure the flow rate of water using a Water Flow Sensor (YF-S201) and Raspberry Pi 3. Useful in smart irrigation and water management systems. ๐Ÿงฐ Components Required Component Quantity Raspberry Pi 3 1 YF-S201 Water Flow Sensor 1 10K Pull-down Resistor 1 Jumper Wires As required Breadboard 1 ⚡ Circuit Connections Sensor Pin Connect To Red (VCC) 5V (Raspberry Pi) Black (GND) GND (Raspberry Pi) Yellow (Pulse Out) GPIO18 (Pin 12) with pull-down resistor ๐Ÿง  Python Code import RPi.GPIO as GPIO import time FLOW_SENSOR = 18 pulse_count = 0 def countPulse(channel): global pulse_count pulse_count += 1 GPIO.setmode(GPIO.BCM) GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback=countPulse) try: while True: pulse_count = 0 time.sleep(1) flow_rate = (pulse_count / 7.5) ...