Related Topics

C Programing
switch (expression) {
case value1:
/* code to execute if expression == value1 */
break;
case value2:
/* code to execute if expression == value2 */
break;
case value3:
/* code to execute if expression == value3 */
break;
default:
/* code to execute if none of the above cases are true */
break;
}
The expression
is the variable or expression whose value is being compared against the set of possible values. The value1
, value2
, and value3
are the possible values that expression
could be equal to. The case
keyword is used to specify each of the possible values. If expression
matches one of the possible values, the code in the corresponding case
block will be executed.
The break
keyword is used to break out of the switch case control structure once a match has been found. Without a break
statement, the code will continue to execute through the subsequent cases until it reaches a break
statement or the end of the switch case control structure.
The default
keyword is used to specify a block of code to execute if none of the possible values match expression
. It is not required to include a default
case, but it can be useful to catch unexpected cases.
Note that the values specified in the case
statements must be of the same data type as the expression
.
#include
int main() {
int day = 5;
switch(day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
In this example, we have a switch statement that takes an integer variable day
as its expression. We then have multiple case statements, each with a different value, that will be executed if the value of day
matches the value specified in the case statement.
In this case, if day
is equal to 5, the code block associated with the fifth case statement printf("Friday\n");
will be executed. If day
is equal to any other value, the code block associated with the default statement printf("Invalid day\n");
will be executed.
Note that each case statement must end with a break
statement to prevent execution from falling through to the next case statement. Also, the default case is not required, but it can be useful to catch unexpected cases.
switch (x) {
case 1:
printf("x is 1\n");
case 2:
printf("x is 2\n");
break;
case 3:
printf("x is 3\n");
break;
}
If the variable x
is set to 1
, the first case will be executed, and then the code in the second case will also be executed because there is no break statement after the first case. However, if x
is set to 2
, only the second case will be executed because there is a break statement immediately after the code in the second case.
This fall-through behavior is sometimes used intentionally to reduce code duplication between case statements, but it can also lead to unintended behavior and bugs if not used carefully. To avoid accidental fall-through, it is recommended to add a comment or use a break
statement at the end of each case statement. In fact, some compilers will issue a warning if they detect a fall-through in a switch statement without an explicit break
or goto
statement.
switch (x) {
case 1:
printf("x is 1\n");
break;
case 2:
printf("x is 2\n");
break;
case 3:
printf("x is 3\n");
break;
}
In this example, if x
is set to 1
, only the code in the first case will be executed, and then the program will exit the switch block because of the break
statement. If x
is set to 2
or 3
, only the code in the second or third case, respectively, will be executed, and then the program will exit the switch block.
The break
statement is also useful for exiting loops and avoiding infinite loops. For example, in a while loop that checks for a certain condition, a break
statement can be used to exit the loop when the condition is no longer true.
In summary, the break
statement in switch cases is used to terminate the execution of the current case and exit the switch block, preventing fall-through behavior and avoiding unintended behavior and bugs.
#include
int main() {
int quantity;
float price, total;
// read input values from user
printf("Enter quantity: ");
scanf("%d", &quantity);
printf("Enter price: ");
scanf("%f", &price);
// calculate total cost before discount
total = quantity * price;
// apply discount based on total quantity
switch (quantity) {
case 0 ... 10:
// no discount
break;
case 11 ... 50:
// apply 5% discount
total *= 0.95;
break;
case 51 ... 100:
// apply 10% discount
total *= 0.9;
break;
default:
// apply 15% discount
total *= 0.85;
break;
}
// display final total cost
printf("Total cost: $%.2f\n", total);
return 0;
}
In this code, the switch statement is used to determine which discount to apply based on the total quantity. The ranges of quantities for each discount level are specified using the “..” range operator in C, and the default case is used to handle any quantity values greater than 100.
Note that the total cost is calculated before the switch statement, and the appropriate discount is applied to the total based on the switch case that matches the quantity. The final discounted total is then displayed to the user.
#include
int main() {
int value = 7;
switch (value) {
case 1:
printf("Value is 1\n");
break;
case 2:
printf("Value is 2\n");
break;
default:
printf("Value is not 1 or 2\n");
break;
}
return 0;
}
In this code, the switch statement is used to compare the value of the value
variable with the cases 1
and 2
. Since value
is not equal to either of these values, the default case is executed, which prints the message “Value is not 1 or 2”.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.