The Switch statement in C

The switch statement is a multi-directional statement used to handle decisions. The switch statement works very much like if… else statement. It is restricted in the way a particular choice is selected, but where it does apply, it provides a very neat and easily understood solution. It’s syntax is:

Syntax:

The expression in the switch statement can be any expression which yields an integer value. For example, integer constant, arithmetic expression, function call returning an integer value, logical, relational, bitwise expressions etc. Expression can also be a character constant because all characters are eventually converted to an integer before any operation. Note that expression can’t be floating point or string.

constant1, constant2 and so on following case keyword must be a constant or constant expression of integer or character type only nothing else. Each case statement must have only one constant , multiple constants are not allowed.

The case can be followed by any number of statements. It is possible that case has no statement under it. If case is followed by multiple statements, then it is not necessary to enclose them in curly braces ({}).

Here are valid switch expression and case constants.

Valid switch expression

Invalid switch expression

Valid case constant

Invalid case constant

How it works:

First of all the expression following 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 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. default is optional if omitted and no case matches then no action takes place.

Let’s now see the switch statement in action.

Expected Output:

How it works:

Let’s say user entered 2. Then the switch expression is evaluated, the value of the expression i.e 2 is compared against every case. When a match is found, all statements under that case are executed. In our case, the value of 2nd case matches the value of the expression, as a result, all the statements under that case are executed. The important thing to note is that statement under case 3, case 4 and default are also executed. This is known as falling through cases and this is the 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, then the statements following the break are not executed and control immediately comes out of the switch statement.

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 switch statement

Expected Output:

1st run:

2nd run:

How it works:

In line 5, two variables a and b of type int are declared.

In line 6, a variable op is declared of type char.

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 line 14, fflush(stdin) function is called to consume the newline character (which was entered while inputting the second number) in the buffer. This call is necessary otherwise the scanf() call in line 17, would read newline character and does not wait for user input.

In lines 16-17, 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 variable op.

In lines 19-35, 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 statements in only that case will be executed. And the break statement causes the control to break out of the switch statement as soon as all the statements under the matching case are executed.

Leave a Comment