| by Arround The Web | No comments

Switch Cases in C

In the C programming language, sometimes we may encounter variables that have different operations for different values. Such a variable is known as the switch case variable. We use the switch case because it has the switch statements and it replaces the if else-if statements in the C. The switch case has one switch expression which is verified by comparing its value with the switch statements that keep on changing. If the value is matched, the statement, in that case, is printed. Otherwise, in the case where there is no match found, the default value is executed after checking the match in all switch cases.

There are some restrictions on the use of these switch cases. First of all, the switch expression is required to have the data type char or integer. The value of each case should be used inside of the switch case statement and its data type must be an integer or const char (constant character).

Procedure

The procedure of this article explains the concept of the switch case in the C language. We follow the sequential procedure where we first learn about the syntax of the switch case. Then, we try to practically solve some examples for the various problems of switch cases.

Syntax

Switch (expression)
{
Case value_1:
// statement to be executed in the case_1
Break; // to come out of the statement
Case value_2:
// statement to be executed in the case_2
Break; // to come out of the statement
.
.
.
Default:
//statement in case of no match with the cases
}

Example 1:
We will try to solve the simplest example where we learn to practically implement the switch cases. Let’s assume that we have an example where we want to first read the input from the user. And whatever the user gives the input, we try to match it with the values of the switch statement. If we find the match with any of the switch statement or switch case value, we execute the statement in that case.

For the implementation, we create a project in the Visual Studio C compiler by selecting the new project from the “files” option in the toolbar. It leads us to the newly created project. Then, we add this project to the path of C repositories using the source file by saving the project name with “c” extension. Once the project is created, we import the “stdio. h” header file as “# include ”. This is done to make sure that the program may use the built-in functions of “stdio” such as printf() and scanf(). Now, we declare the main function with a return type integer. In the body of this main function, we initialize a variable “num” having the data type “int” and value “0” assigned to it. We take the value for this variable num from the user and this purpose. We call the scanf() method and pass the address of the num as “&num” and the format specifier as “%d”. This allows the num to take its value from the user input.

To impose the conditions on the value of this num, we use the switch case statements and we pass the num to the switch expression and define a first switch case with value 20 in its statement. We print the expression “num is 20” and then break the statement. We define three to four such switch cases. At last, we define the default statement that prints the expression “num is not a match with any switch case” if we did not find the match of the user input num with any of the switch case values. This example is implemented in the form of a program in C as:

#include<stdio.h>  
int main() {
    int num = 0;
    printf("enter a number:");
    scanf("%d", &num);
    switch (num) {
    case 20:
        printf("num is 20");
        break;
    case 30:
        printf("num is 30");
        break;
    case 50:
        printf("num is 50");
        break;
    default:
        printf("num is not equal to 20, 20 or 50");
    }
    return 0;
}

We take the user input for num as “5”. This value does not match with any values in the switch case. Hence, the default statement is displayed in the output. If we give the user input num as “20”, this value matches the first switch case and the statement of the first switch case is executed and displayed in the output.

Example 2:
The second example in this guide will try to implement another example using the switch case in the C language. We start by first creating the project in the Visual Studio Code and then importing the library “# include <stdio.h>” for the print and scan functions. After this step, we create a main function with the return type “int”. We initialize the two variables “a” and “b” with the values “20” and “10” respectively. In the switch expression, we pass the condition if “a” is greater than “b”. If the sum of both of these variables “a” and “b” is also greater than “0”, we will have the first switch case with the Boolean value “1” as “true”. If the switch expression is true, it prints the expression as true and the second switch case will have a value “0”. If the switch expression is wrong, the default statement is executed. If the expression is not get true or false, the default statement prints the expression “nothing”.

#include<stdio.h>    
int main ()
{
    int a= 20, b = 10;
    switch (a > b && a + b > 0)
    {
    case 1:
        printf("true");
        break;
    case 0:
        printf("false");
        break;
    default:
        printf(" nothing ");
    }
}

We executed the previously-explained example in the form of the program. The output of the program came out to be true as the switch expression matched with the first switch case. So the output displays “true” as its statement.

Conclusion

The switch case in the C language is used when we have more than one option for the single variable that we need to execute. The switch finds the best match for the switch expression and executes the statement accordingly. If we don’t find any match at all for the switch expression, the default statement is executed. We implemented the two such examples for the switch case in C with a complete description of the correct use of the syntax for the switch case.

Share Button

Source: linuxhint.com

Leave a Reply