Introduction to C
Data Types in C
Operators in C
Hello World Program
Loops in C
Single Dimensional Array
Two Dimesnional Arrays
Array Example Programs
'switch' is a keyword in C used to execute a block of code on the basis of selection from multiple choices. The switch-case statement is a multi-way decision statement. Unlike the multiple decision statement that can be created using if-else the switch statement evaluates the conditional expression and tests it against numerous constant values.
Syntax for the switch statement is as follows:
Explanation for the syntax
In C switch statement, the selection is determined by the value of an expression that you specify, which is enclosed between the parentheses after the keyword switch. The data type of value, which is returned by expression, must be an integer value otherwise the compiler will issue an error message.
When the break statement is executed, it yields to come out from the switch. In this case, the block of code in that case only will be executed. However, its not necessary to use break in each case. But if you will not use break, then it will execute the next cases also in a sequence.
Suppose if all case statements are not satisfied by the expression, in that case, default statement will be executed.
Now, let us understand the working of switch with a program.
C Program to find the sum, subtraction, multiplication and division of two numbers using switch
Note: If find any issues/errors with the tutorial above, please immediately mail it to firstname.lastname@example.org