Switch Case Program

A switch statement tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed.

Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is found, then the default statement is executed, and the control goes out of the switch block.

The switch statement allows us to execute one code block among many alternatives. You can do the same thing with the if...else..if ladder. However, the syntax of the switch statement is much easier to read and write.

Syntax Of Switch Case Program

  • The expression can be an integer expression or character expression.
  • Value-1, 2, n are case labels that are used to identify each case individually. Remember that case labels should not be the same as it may create a problem while executing a program. Suppose we have two cases with the same label as ‘1’. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and does not provide the desired output.
  • Case labels always end with a colon ( : ). Each of these cases is associated with a block.
  • A block is nothing but multiple statements that are grouped for a particular case.
  • Whenever the switch is executed, the value of test-expression is compared with all the cases which we have defined inside the switch. Suppose the test expression contains value 4. This value is compared with all the cases until the case whose label four is found in the program. As soon as a case is found the block of statements associated with that particular case is executed and control goes out of the switch.
  • The break keyword in each case indicates the end of a particular case. If we do not put the break in each case then even though the specific case is executed, the switch will continue to execute all the cases until the end is reached. This should not happen; hence we always have to put break keyword in each case. Break will terminate the case once it is executed and the control will fall out of the switch.
  • The default case is an optional one. Whenever the value of test-expression is not matched with any of the cases inside the switch, then the default will be executed. Otherwise, it is not necessary to write default in the switch.
  • Once the switch is executed the control will go to the statement-x, and the execution of a program will continue.

Switch Case Program Example

Output:

Code Explanation

  • In the given program we have initialized a variable num with value 8.
  • A switch construct is used to compare the value stored in variable num and execute the block of statements associated with the matched case.
  • In this program, since the value stored in variable num is eight, a switch will execute the case whose case-label is 8. After executing the case, the control will fall out of the switch and the program will be terminated with the successful result by printing the value on the output screen.

Why do we need a Switch case?

There is one potential problem with the if-else statement which is the complexity of the program increases whenever the number of the alternative paths increases. If you use multiple if-else constructs in the program, a program might become difficult to read and comprehend. Sometimes it may even confuse the developer who himself wrote the program. The solution to this problem is the switch statement.

Rules for switch statement:

  • An expression must always execute to a result.
  • Case labels must be constants and unique.
  • Case labels must end with a colon ( : ).
  • A break keyword must be present in each case.
  • There can be only one default label.
  • We can nest multiple switch statements.

Summary

  • A switch is a decision making construct in ‘C.’
  • A switch is used in a program where multiple decisions are involved.
  • A switch must contain an executable test-expression.
  • Each case must include a break keyword.
  • Case label must be constants and unique.
  • The default is optional.
  • Multiple switch statements can be nested within one another.

Top Book Suggestion

The two books on C Programming Language that I Personally Recommend. I really loved reading these books. These books are listed for quality content, easy steps, and affordable price. You can get it from Amazon and Flipkart. 

 

Check The Price On Amazon:

1. C Programming Beginner’s Guide

2. Head First C: A Brain-Friendly Guide

Check The Price On Flipkart:

1. Introduction to C Programming 

2. Head First C: A Brain-Friendly Guide

Buy Premium Courses At Lowest Price. Become A Certified Developer Today!

Important C Programs

Pin It on Pinterest

Share This