The Switch statement in C

The Switch Statement

The switch statement is a multi-directional statement used to handle decisions. It works almost exactly like the
if-else statement. The difference is that the switch statement produces a more readable code in comparison to if-else statement. Also, sometimes it executes faster than the if-else counterpart. The syntax of switch statement is as follows:

Syntax:

The expression in the switch statement can be any valid expression which yields an integral value. The expression can also be a character constant ( because all characters are eventually converted to an integer before any operation ) but it can’t be floating point or string.

constant1, constant2 and so on following the case keywords must be of integer type (like int, long int etc ) or character type. It can also be an expression which yields an integer value. Each case statement must have only one constant. Multiple constants in the single case statement are not allowed. Further, all case constants must be unique.

After each case constant, we can have any number of statements or no statement at all. If there are multiple statements then you don’t need to enclose them with the braces ({}).

Here are some valid switch expressions and case constants.

Valid switch expression

Invalid switch expression

Valid case constant

Invalid case constant

How it works:

First of all, the expression following the switch is evaluated, then the value of this expression is compared against every case one by one. If the value of the expression matches with any case constant then the statements under that case are executed. If the value of the expression does not match any case constants then the statements under default are executed. The default statement is optional if omitted and no case matches then no action takes place.

Let’s now see the switch statement in action.

The following program asks the user to enter a number and prints the message accordingly:

Expected Output:

How it works:

Let’s say user entered 2. Then the switch expression is evaluated, the value of the expression is compared against every case. When a match is found, all statements under that case are executed. In our case, the value of the 2nd case matches the value of the expression (i.e 2). As a result, all the statements under that case are executed. The important thing to note is that that statements under case 3, case 4 and default are also executed. This is known as falling through cases and this is how the switch statement works by default.

Most of the time, we don’t want the control to fall through the statements of all the cases, instead, we want to just execute the statements under the matching case and break out of the switch statement. We can achieve this using the break statement. When the break statement is countered inside the switch statement, the program control immediately breaks out of the switch and resumes execution with the statement following it.

Let’s rewrite our previous program, but this time, only the statements of the matching case will be executed.

Expected Output:

1st run:

2nd run:

A Simple Calculator using the Switch statement

Expected Output:

1st run:

2nd run:

How it works:

In line 5, two variables a and b of type int are declared. These variables will store the numbers entered by the user.

In line 6, a variable op of type char is declared. This will store the sign of the operator entered by the user.

In lines 8-9, the program asks the user to enter the first number. The entered number is stored in the variable a.

In lines 11-12, the program again asks the user to enter the second number. The entered number is stored in the variable b.

In lines 14-15, the program asks the user to enter the symbol of the operation he/she wants to perform on the two numbers. The entered symbol is assigned to the variable op.

In lines 17-33, we have a  switch statement. The variable op is used as an expression in the switch statement. The value of op is then compared against every case one by one. If the value of the op matches any case then the statements in that case are executed and the break statement causes the program control to break out of the switch statement.

Leave a Comment

%d bloggers like this: