C Switch StatementThe switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles 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. The syntax of switch statement in c language is given below: Rules for switch statement in C language
Let's try to understand it by the examples. We are assuming that there are following variables.
Flowchart of switch statement in CFunctioning of switch case statementFirst, 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. No two cases can have similar values. 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. How does C switch statement work?Let's go through the step-by-step process of how the switch statement works in C: Consider the following switch statement: C Program: Output Value is 2 Step-by-step Process:
Example of a switch statement in CLet us see a simple example of a C language switch statement. Output enter a number:4 number is not equal to 10, 50 or 100 enter a number:50 number is equal to 50 Switch case example 2Output hi Break and Default keyword in Switch statementLet us explain and define the "break" and "default" keywords in the context of the switch statement, along with example code and output. 1. Break Keyword:The "break" keyword is used within the code block of each case to terminate the switch statement prematurely. When the program encounters a "break" statement inside a case block, it immediately exits the switch statement, preventing the execution of subsequent case blocks. The "break" statement is crucial for avoiding switch statements' "fall-through" behavior. Example: Let's take a program to understand the use of the break keyword in C. Output Value is 3 Explanation: In this example, the switch statement evaluates the value of the variable num (which is 3) and matches it with case 3. The code block associated with case 3 is executed, printing "Value is 3" to the console. The "break" statement within case 3 ensures that the program exits the switch statement immediately after executing this case block, preventing the execution of any other cases. 2. Default Keyword:When none of the case constants match the evaluated expression, it operates as a catch-all case. If no matching case exists and a "default" case exists, the code block associated with the "default" case is run. It is often used to handle circumstances where none of the stated situations apply to the provided input. Example: Let's take a program to understand the use of the default keyword in C. Output Value is not 1, 2, or 3 Explanation: In this example, the switch statement examines the value of the variable num (which is 5). Because no case matches the num, the program performs the code block associated with the "default" case. The "break" statement inside the "default" case ensures that the program exits the switch statement after executing the "default" case block. Both the "break" and "default" keywords play essential roles in controlling the flow of execution within a switch statement. The "break" statement helps prevent the fall-through behavior, while the "default" case provides a way to handle unmatched cases. C Switch statement is fall-throughIn C language, the switch statement is fall through; it means if you don't use a break statement in the switch case, all the cases after the matching case will be executed. Let's try to understand the fall through state of switch statement by the example given below. Output enter a number:10 number is equal to 10 number is equal to 50 number is equal to 100 number is not equal to 10, 50 or 100 Output enter a number:50 number is equal to 50 number is equal to 100 number is not equal to 10, 50 or 100 Nested switch case statementWe can use as many switch statement as we want inside a switch statement. Such type of statements is called nested switch case statements. Consider the following example. Output the value of i evaluated in outer switch: 10 The value of j evaluated in nested switch: 20 Exact value of i is : 10 Exact value of j is : 20 Advantages of the switch statement:There are several advantages of the switch statement in C. Some main advantages of the switch statement are as follows:
The switch statement supports using a default case which serves as a catch-all option for values that do not match any provided cases. This default case handles unusual inputs or circumstances that are not expressly stated. Disadvantages of the switch statement:There are several disadvantages of the switch statement in C. Some main disadvantages of the switch statement are as follows:
Next Topicif-else vs switch |