• switch case in c programming questions
  • switch case with do you want to continue in c
  • A switch case is a control statement in C that allows you to choose between several different options based on the value of a particular expression. 
  • C has a built-in multi-way decision statement known as a switch. It is a keyword we can select a single option from multiple options. The switch  is having expression; each and every option is known as case and must be followed by colon.
  • Each and every case ends with break statement.
  • The last case is default even that will have break.
  •  The break is a keyword. Each and every case ends with break statement

 SYNTAX:-

 switch(expression)
 {
    case value-1 :block-1 break;
    case value-2 :block-2 break;
    case value-3 :block-3 break;
    .........
    .........
    .........
    .........
    default :default-block break;
 }

Here is an example of how to use a switch case in a C program:

#1; switch case in c programming questions

  1. # include <stdio.h>
  2. void main() {
  3.     char operator;
  4.     double a,b;
  5.     printf("Enter an operator (+, -, *,): ");
  6.     scanf("%c", &operator);
  7.     printf("Enter two numbers ");
  8.     scanf("%lf %lf",&a, &b);
  9.     switch(operator)
  10.     {
  11.         case '+':
  12.             printf("%.1lf + %.1lf = %.1lf",a, b, a + b);
  13.             break;
  14.         case '-':
  15.             printf("%.1lf - %.1lf = %.1lf",a, b, a - b);
  16.             break;
  17.         case '*':
  18.             printf("%.1lf * %.1lf = %.1lf",a, b, a * b);
  19.             break;
  20.         case '/':
  21.             printf("%.1lf / %.1lf = %.1lf",a, b, a / b);
  22.             break;
  23.         default:
  24.             printf("Error! operator is not correct");
  25.     }
  26. }

Output:
  1. Enter an operator (+, -, *,): +
  2. Enter two numbers 12
  3. 12
  4. 12.0 + 12.0 = 24.0
  5. Process returned 18 (0x12)   execution time : 7.392 s
  6. Press any key to continue.



  • In the below  example, the program prompts the user to enter a number and then uses a switch case to determine what to do based on the value of the number. If the user enters 1, the program will print "You entered 1.
  • " If the user enters 2, the program will print "You entered 2," and so on. If the user enters a value that is not covered by any of the cases, the default case will be executed.



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu