Ever wondered how to make your C code cleaner? Enter the "switch" statement! It's like a superhero keyword that simplifies handling multiple conditions. The switch statement in C is a handy tool for handling multiple conditions in a more organized way. Syntax of switch statement in C is straightforward.
In this article, we will learn about the syntax of switch statement in C language.
What are Switch Statements in C language?
Switch statements are conditional statements that are used to choose between many conditions which are being used in the switch statement. These statements provide control in making a decision or comparing the result.
Syntax of Switch Statements in C
Syntax of Switch Statements in C contains the cases which have to check while running the switch statement. The default statement is also present in the syntax of switch statement in C, which is run when all the other cases are false.
Let’s see the code of the syntax of switch statement in C.
switch (<Expression>) {
case <Value1> : // If this condition is true
<Statement>;
break;
case <Value2> : // If this condition is true
<Statement>;
break;
.
. //So on.
default : // If none of the conditions are true
<Statement>;
break;
}
The above code is an example of a switch statement to understand the syntax used in a switch statement. Let's see this syntax one by one.
Switch keyword, along with the parenthesis, is used with an expression inside this parenthesis.
Case keyword is used along with the constant expression and colon, as used in the above code.
Break statement is used to break the switch statement. I.e., when a case is matched with the condition required, the break keyword allows it to move out of the switch statement instead of executing other cases.
Default keyword gives a particular output when any mentioned cases don't match the required condition.
Working of Switch statements
As the syntax of a switch statement in C tells us about an expression inside the switch statement, the first step is to calculate a constant value from the expression inside the switch statement.
Now, it matches that constant value with the value of each case; if the value is matched, then the statement inside that case will run, and the code will come out of the switch, as there is a break statement after every switch case.
While if there is no match in any of the cases, then the statement in the default condition will run.
Let’s take a look at some of the examples on the switch statements. Also read - Bit stuffing program in c
Example
Write a code using the syntax of a switch statement in C to verify if a number, when divided by 10, will leave the remainder of 1, 2, or 3. If these three numbers are not the remainder, then print this is different than the required number.
Code in C
C
C
#include <stdio.h>
int main() { int a = 11; switch(a % 10){ case 1: printf("My remainder is 1"); break; case 2: printf("My remainder is 2"); break; case 3: printf("My remainder is 3"); break; default: printf("This is different than the required number."); } return 0; }
The above code shows the use of the switch statement in which we code to find the remainder of a number when divided by 10 is 1, 2, or 3, or the remainder is other than these three numbers.
When we use the switch statement inside it, it is known as a nested switch statement. This means if you want to use a switch statement inside a particular case, then it can be done by a nested switch.
Syntax of Nested Switch
switch(Expression_1) {
case <Val1> :
printf("Another Switch begins" );
switch(Expression_2) {
case <Val_1.1> :
printf("Nested Switch" );
break;
case <Val_1.2>' :
<Statement>
break;
default:
<Statement>
}
break;
case <val 2>:
<Statement>
break;
default:
<Statement>
break;
}
The above is the syntax in which all the keywords like a switch, case, break, and default are the same as the usual switch statement, but one switch is inside the others, so it is the nested switch.
Example of a Nested Switch
Write a code if the number is divided by 7, then is it dividing by 5 or not by using the nested switch.
Code in C
C
C
#include <stdio.h>
int main() { int a = 35; switch(a % 7){ case 0: switch(a % 5){ case 0: printf(“This number is divided by both 5 and 7”); break; default: printf("This is divided by 7 but not 5."); } break; default: printf("This is not divided by 7."); } return 0; }
The above code first enters the first switch statement and checks the case 0, then enters the other switch condition to check whether the number is also divisible by 5 or not and prints the output accordingly.
Flowchart of the Switch Statement in C
A flowchart of a switch statement in C typically starts with a decision box containing the switch keyword and the expression to be evaluated. Arrows then lead to individual process boxes for each case, detailing the code to execute if the expression matches a particular value. Each case is connected to the next, and a default case may be included for unmatched values. The flowchart illustrates the sequential evaluation of cases, with optional breaks to exit the switch statement when a match is found. The process concludes with a terminator symbol.
What are the Important Keywords of the Switch Statement:
Break: The break statement stops the execution of the other cases when we get a correct case in the switch statement. It emerges from the switch when it finds the correct cases and executes the break keyword.
Default: While the default keyword is used to run the particular piece of code when none of the given cases are not completing the required condition, we don’t use the break keyword in the default case because it is the end of the switch statement.
What happens if you don't use the break statement in the Switch case?
As we learned, that break statement prevents the code from checking the other cases when the required case is found. If we don’t use the break statement, all the cases after the required cases will be executed.
Let's see an example.
Code in C
C
C
#include <stdio.h>
int main() { int a = 11; switch(a % 10){ case 1: printf("My remainder is 1 \n");
case 2: printf("My remainder is 2 \n");
case 3: printf("My remainder is 3 \n");
default: printf("This is different than the required number."); } return 0; }
What is a syntax of switch statement in Visual Basic?
In Visual Basic, the switch statement is expressed using the Select Case structure. The syntax involves evaluating an expression with multiple cases, each specifying a value or condition, followed by code execution.
What is the difference between conditional and iterative statements?
Conditional statements are used when we have to check whether a particular condition is true or false, while in iterative statements, code is repeatedly run until the statement is true.
How many conditional statements are there in the C programming language?
There are two types of conditional statements in the C programming language. First is the if..else statement, which helps to run a statement if it is true or another statement if that is false.
What is the default keyword in the syntax of switch statement in C?
In a switch statement, first of all, all the cases mentioned inside the switch statement are checked if any of them will not satisfy the condition in each case, then the statements inside default will execute.
Conclusion
In this article, we learned about switch statement syntax in C language. We also discussed the working of the switch statement and the concept of the nested switch statement.
We also saw that a switch statement is conditional but not iterative, as it checks each condition one by one instead of checking the same condition repeatedly.