C Switch Statement
The switch statement in C is an alternate to if else-if ladder statement which allows us to execute multiple operations for the different possible values of a single variable called switch variable. Here, we can define various statements in the multiple cases for the different values of a single variable.
Rules for switch statement in C language
- The switch expression must be an integer or character type.
- The case value must be an integer or character constant.
- The case value can be used only inside the switch statement.
- The break statement in switch case is not must. It is optional. If there is no break statement found in the case, all the cases will be executed present after the matched case. It is known as the fall through the state of C switch statement.
Functioning of switch case statement
First, the integer expression specified in the switch statement is evaluated. This value is then matched one by one with the constant values given in the different cases. If a match is found, then all the statements specified in that case are executed along with the all the cases present after that case including the default statement. If the matched case contains a break statement, then all the cases present after that will be skipped, and the control comes out of the switch. Otherwise, all the cases following the matched case will be executed. No two cases can have similar values.
In C language, the switch statement is fall through; it means if we don't use a break statement in the switch case, all the cases after the matching case will be executed.
#include <stdio.h>
int main()
{
int number;
printf("Enter a number : ");
scanf("%d", &number);
switch (number){
case 10:
printf("Number is equals to 10.");
break;
case 50:
printf("Number is equals to 50.");
break;
case 100:
printf("Number is equals to 100.");
break;
default:
printf("Number is not equal to 10, 50 and 100.");
}
return 0;
}
Output :
Enter a number : 50
Number is equals to 50.
Enter a number : 75
Number is not equal to 10, 50 and 100.
#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch (x < y && x+y > 0){
case 1:
printf("Hi!!!");
break;
case 0:
printf("Bye!!!");
break;
}
return 0;
}
Bye!!!
Program to perform Arithmetic Operations of two numbers
#include <stdio.h>
int main()
{
int a, b, choice;
printf("Enter two numbers : ");
scanf("%d %d", &a, &b);
printf("Enter 1 for addition\nEnter 2 for subtraction\nEnter 3 for multiplication\nEnter 4 for getting remainder\n");
scanf("%d", &choice);
switch (choice){
case 1:
printf("Sum of given numbers is : %d", (a+b));
break;
case 2:
printf("Subtraction of given numbers is : %d", (a-b));
break;
case 3:
printf("Multiplication of given numbers is : %d", (a*b));
break;
case 4:
printf("Remainder of given numbers is : %d", (a%b));
break;
}
return 0;
}
Enter two numbers : 4 2
Enter 1 for addition
Enter 2 for subtraction
Enter 3 for multiplication
Enter 4 for getting remainder
1
Sum of given numbers is : 6
Enter two numbers : 4 2
Enter 1 for addition
Enter 2 for subtraction
Enter 3 for multiplication
Enter 4 for getting remainder
2
Subtraction of given numbers is : 2
Enter two numbers : 4 2
Enter 1 for addition
Enter 2 for subtraction
Enter 3 for multiplication
Enter 4 for getting remainder
3
Multiplication of given numbers is : 8
Enter two numbers : 4 3
Enter 1 for addition
Enter 2 for subtraction
Enter 3 for multiplication
Enter 4 for getting remainder
4
Remainder of given numbers is : 1
Comments
Post a Comment