Conditional Statements : Switch Case

In the previous tutorial, we’ve discussed about if – else and nested if – else statements and how we can use them to test certain conditions and execute a block of code depending on whether the condition becomes true or false. In this tutorial, we’ll be covering another Conditional Statement used in C known as Switch Case. The major difference between choosing to use if-else and switch is, when we have to compare a single variable to different set of values, then Switch is preferred, and if-else if preferred otherwise.

Conditional Statements : Switch Case C language

Conditional Statements : Switch Case C language

The basic semantics of a switch block statement is as follows :

Here variable-name corresponds to the variable you want to compare with a set of values and inside the curly braces we have defined cases such that if value of variable is 1 then the statement 1 and statement 2 would execute, for variable having value 2, statement 3 and 4 would execute and so on. If none of the cases are true, then statement n corresponding to default would execute.

Let us take an example and understand how it’s done. Let us consider we need to just print what number it is if the number is less than or equal to 5 else print number is not 1, 2, 3, 4 or 5. We’ll consider that user has input a number which is stored in variable named num. This can be done through if – else statements as we studied earlier like this :

Now in the above case, since we need to compare just one variable with a set of values, this can be done easily through switch case as follows:

Notice here that I have mentioned break statement after each case , this is to terminate the the case above it if it’s executed. Let’s see if we didn’t include break what would happen, so consider this code snippet.

Now, if we run this and input 2 is given then output would be as shown below

Switch case in C language Output Screen

Switch case in C language Output Screen

See, we not only got Number is 2 statement printed, but got all statements printed after that case, in a sequential manner, this is because in a switch condition we need to have conditional flow of statement using break statement.

Now, if you include break after each case but doesn’t do it with default case, it would run fine too, this is because if the default case is true, then after executing set of statements with that case, the control would automatically be out of the switch block, so technically we do not need break after default case.

Now let’s see how to match a character type variable. Consider a character (char) type variable named character_variable , and if we need to use switch, it can be done in this way:

Here, notice that we have enclosed the character value between single quotes ( ‘   ‘ ).

Like wise, you can design your switch case for various purpose as/when you need to compare a single variable value with a set of values.

In next tutorials we’ll know about the Storage Classes in C. If you have any problem with anything till now, feel free to let us know through comments section below, we’ll be glad to help you.

Keep Coding !

Leave a Reply

Your email address will not be published. Required fields are marked *