Switch Statement in C Language with Example Programs

Spread the love

Switch-Statement-in-C-Programming-Language-with-Examples

Introduction:

We have discussed the Nested-if-else and if else ladder in an earlier article. In today’s article, we will look at the Switch Statement in C language.

Switch Statement:

The Switch statement is a multi-decision statement. We can use the switch statement to compare the given expression (Switch condition) with multiple conditions(Cases) and execute the respective code block.

Syntax-of-switch-statement-or-switch-case-in-c-programming

The switch statement is an alternative to the if-else ladder. but the readability of the switch statement is much better compared to if else ladder.

The switch statement is also called the Switch case.

Syntax of Switch statement:

As you can see from the above syntax, If the given expression is evaluated against all case

constant_expression’s and if expression matches any constant_expression then that specific block of code will be executed.

For example, If the given expression matched the constant_expression_2 then The case 2 Code block up to the break statement will be executed.

The break statement is used to break out( or come out) of the switch case.

📢 The break statement is optional, But if you forgot to specify the break then the following cases are also executed.

The default case is optional. If the given expression is not matched any case then, Then the default case will be executed.

If the expression not matched any case and the switch doesn’t have default case, Then no action will be taken and program execution goes out of the switch statement.

📢 The switch expression must be of an integer or character type. (Float values are not allowed)

The Case Expression should be Integer or character constant.

When there are many options to pick from, we can use the switch statement or the if-else ladder. However, as the number of possibilities grows, the complexity of the if-else ladder also grows, reducing code readability and occasionally leading to confusion. Therefore, in suitable instances, it is advised to use the switch case rather than the if-else ladder.

To further understand the switch statement, let’s look at an example.

We’re going to use the switch statement to make an arithmetic calculator.

Program to understand switch statement – Arithmetic Calculator:

Program Output – Calculator:

Switch Statement Calculator Program Explanation:

The above program will take the two numbers and desired operator from the user, And perform the selected operation.

We took the two numbers from the users num1 , num2, and the Operator op .

Then we used the switch statement to compare the given operator op against the Addition( +), Subtraction( -), Multiplication( *), and Division( /) cases.

In the above example, The user entered 20 and 10 as the input values and + as the operator ( op).

The switch expression op (operator) will be compared with all cases, If op matches any case, The block of code until the break will be executed, Here switch found the match at the first case +, and executed the case '+' code block.

Similarly, If we provide the Invalid operator, The default case will be executed.

Switch statement to check character is vowels and consonants:

We have earlier discussed that, If you don’t specify the break after each case, The subsequent case also executes. This often causes bugs but it is also useful to create operator OR like behavior.

To understand better lets look at the following program.

We are going to use the switch case to detect the Vowels and Consonants.

Program output:

We used gcc compiler to compile the code and renamed the executable file as checkVowels

As you can see from the code, We have intentionally not used break statement after the cases a, e, i , o

Finally, we included the break statement for the case u.

As the break statement does not exist in the above cases, It becomes like an OR operation. This means even if the user enters any of a, e, i, o, u alphabets, The following code block will be executed(till break).

And finally, we have the default case, If the user enters any other alphabet other than a, e, i, o, u, Then the default case will be executed.

Nested Switch Statement or Switch Case:

We can have Nested Switch statements in the C programming language.

Syntax of Nested Switch Statement

As you can see from the syntax above, We can have a Nested switch statement inside the Switch statement.

Here is the program to understand the Nested Switch statement.

Program to understand Nested Switch Statement in C:

We are going to write a Food Order-taking program. The program will ask if the user is Veg or Non-Veg.

Depending upon the user option, We will further present the customizations to the user.

Food Order Program output:

Here we used the two switch statements inside the main switch statement.

Sometimes having more nested switch statements can cause confusion, So Make sure you are properly using the break statement at the end of each case.

Exercise Program:

In the earlier posts, We looked at the Grade of the student’s program, But we used the if else ladder. You can try to re-write the Grade of the students program using the Switch statement.

Here is the link to Grade of the students program.

Conclusion:

We have discussed about the switch statement in C programming along with the Nested switch statements and how the break statement affects the flow of the program.

Tutorials Index:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

Leave a Reply