Switch Case Statements
Switch Case Statements
The control statement that allows us to make a decision effectively from the number of choices is called a switch
, or a switch case-default
since these three keywords go together to make up the control statement. The expression in switch
returns an integral value, which is then compared with different cases. Switch
executes that block of code, which matches the case value. If the value does not match with any of the cases, then the default
block is executed.
Following is the syntax of switch case-default
statements:
switch (integer expression) {
case {value 1}:
do this;
break;
case {value 2}:
do this;
break;
case {value 3}:
do this;
break;
default:
do this;
}
Understanding the Syntax:
- The expression following the
switch
can be an integer expression or a character expression. - The
case value 1
and2
are case labels that are used to identify each case individually. Case labels should be unique for each of the cases. - At the end of the case labels, a colon (:) is used.
- Each case is associated with a block containing multiple statements.
- Whenever the
switch
is executed, the value of the test-expression is compared with all the cases present. When a case is found, the block of statements associated with that case will execute. - The
break
keyword indicates the end of a particular case. Ifbreak
is not used in each case, C'sswitch
will continue to execute all the cases until the end is reached. - The
default
case is optional. It will be executed when the expression's value does not match any of the cases inside theswitch
.
One example where we could use the switch
case statement is:
#include <stdio.h>
int main() {
int i = 2;
switch (i) {
case 1:
printf("Statement 1");
break;
case 2:
printf("Statement 2");
break;
case 3:
printf("Statement 3");
break;
default:
printf("No valid i to switch to.");
break;
}
return 0;
}
Output:
Statement 2
Different from if-else. How?
There is one problem with the if statement: the program's complexity increases whenever the number of if statements increases. If we use multiple if-else statements, the code might become difficult to read and comprehend. Using the switch
statement is the solution to this problem.
Furthermore:
- Switch statements cannot evaluate float conditions; the test expression can only be an integer or a character, whereas if statements can evaluate float conditions.
- Switch statements cannot evaluate relational operators; they are not allowed in switch statements, whereas if statements can evaluate relational operators.
- Cases in the switch can never have variable expressions; for example, we cannot write
case a + 3:
Rules for Switch Statements:
- The test expression of
switch
must necessarily be anint
orchar
. - The value of the case should be an integer or character.
- Cases should only be inside the
switch
statement. - Using the
break
keyword in theswitch
statement is not necessary. - The case label values inside the
switch
should be unique.
It is not necessary to use the break
keyword after every case. Break
should only be used when we want to terminate our case at that time; otherwise, we won’t. We can also use nested switch statements, i.e., switch
inside another switch
. Also, the case constants of the inner and outer switch may have common values without any conflicts.