Skip to main content

C switch...case Statement

In this tutorial, you will learn to write a switch statement in C programming (with an example).
The if..else..if ladder allows you to execute a block code among many alternatives. If you are checking on the value of a single variable in if...else...if, it is better to use switch statement.
The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.

Syntax of switch...case

switch (n)
​{
    case constant1:
        // code to be executed if n is equal to constant1;
        break;

    case constant2:
        // code to be executed if n is equal to constant2;
        break;
        .
        .
        .
    default:
        // code to be executed if n doesn't match any constant
}
When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case.
Suppose, the value of n is equal to constant2. The compiler executes the statements after case constant2: until break is encountered. When break statement is encountered, switch statement terminates.

switch Statement Flowchart



Example: switch Statement

  1. // Program to create a simple calculator
  2. #include <stdio.h>
  3. int main() {
  4. char operator;
  5. double firstNumber,secondNumber;
  6. printf("Enter an operator (+, -, *, /): ");
  7. scanf("%c", &operator);
  8. printf("Enter two operands: ");
  9. scanf("%lf %lf",&firstNumber, &secondNumber);
  10. switch(operator)
  11. {
  12. case '+':
  13. printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
  14. break;
  15. case '-':
  16. printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
  17. break;
  18. case '*':
  19. printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
  20. break;
  21. case '/':
  22. printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/secondNumber);
  23. break;
  24. // operator doesn't match any case constant (+, -, *, /)
  25. default:
  26. printf("Error! operator is not correct");
  27. }
  28. return 0;
  29. }
Output
Enter an operator (+, -, *,): -
Enter two operands: 32.5
12.4
32.5 - 12.4 = 20.1
The - operator entered by the user is stored in operator variable. And, two operands 32.5 and 12.4 are stored in variables firstNumber and secondNumber respectively.
Then, control of the program jumps to
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
Finally, the break statement terminates the switch statement.

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

short key of c programming

short key of c programming Shortcut Keys(Hot Keys)     Operation    Alt + F  -            Open file Alt + F3 -           Close Alt + F + N  -    New file Alt + X  -           Exit turbo c Alt + F5 -          Output Screen Alt + F + L  -    Check programme load or not. Alt + ENTER  - Full screen Shift + Del -       Cut Shift + Insert -   Paste Ctr + Insert -    Copy Alt+F9 -           Compile a program Ctr + F8           Bbreak point Ctrl+F9 -          To run a program Ctrl+s -...

Unordered List, Nested lists, Description List,TUTE-8

                                                             Unordered List An unordered list can be created with the <ul> tag and each list item can be created with the <li> tag as shown by the example below: <ul>   <li>Item</li>   <li>Another Item</li>   <li>Yet Another Item</li>  </ul> This will produce a bulleted list (which is the default style): You should use ul to display a list of items, where the order of the items is not important. If changing the order of the items makes the list incorrect, you should use <ol>.   Nested lists You can nest lists to represent sub-items of a list item. <ul>   <li>item 1</li>   <li>item 2     <ul>    ...