Coupon Accepted Successfully!


The switch Statement

The switch statement is a multi-way branch statement and an alternative to if-else-if ladder in many situations. The expression of switch contains only one argument, which is then checked with a number of switch cases. The switch statement evaluates the expression and then looks for its value among the case constants. If the value is matched with a particular case constant, then those case statements are executed until a break statement is found or until the end of switch block is reached. If not, then simply the default (if present) is executed (if a default is not present, then the control flows out of the switch block). The default is normally present at the bottom of the switch case structure. But we can also define default statement anywhere in the switch structure. The default block must not be empty. Every case statement terminates with a ‘:’ (colon). The break statement is used to stop the execution of succeeding cases and pass the control to the end of the switch block.

The syntax of the switch case statement is shown in Figure.

The switch case.


Note the following for switch case.

  1. The switch expression: In the block the variable or expression can be a character or an integer. The integer expression following the keyword switch will yield an integer value only. The integer may be any value 1, 2, 3, etc. In case of character constant, the values may be with alphabets such as ‘x’, ‘y’, ‘z’, etc.
  2. The switch organisation: The switch expression should neither be terminated with a semicolon (;) nor with any other symbol. The entire case structure following the switch should be enclosed within curly braces. The keyword case is followed by a constant. Every constant terminates with a colon (:). Each case statement must contain different constant values. Any number of case statements can be provided. If the case structure contains multiple statements, they need not be enclosed within curly braces. Here, the keyword case & break performs, respectively, the job of opening and closing curly braces.
  3. The switch execution: When one of the cases is satisfied, the statements following it are executed. In case there is no match, the default case is executed.
  4. The break statement used in switch passes control outside the switch block. By mistake if no break statements are given, all the cases following it are executed.


The flowchart for the switch case is as shown in Figure.

Description: 4245.png

Flowchart for switch case.

Programs on switch case are as follows.

5.13 Write a program to print lines by selecting the choice.


Explanation: In the above program a menu appears with five options and it requests the user to enter his/her choice. The choice entered by the user is then passed to switch statement. In the switch statement the value is checked with all the case constants. The matched case statement is executed in which the line is printed according to the user’s choice. If the user enters a non-listed value, then no match occurs and the default is executed. The default warns the user with a message “Invalid Choice.”

5.14 Write a program to perform the multiple arithmetic operations as l. Addition, 2. Subtraction, 3. Multiplication, and 4. Division using switch case.


Explanation: The above program is used to perform various arithmetic operations. It requests the user to enter the choice. The choice entered by the user is checked with the if statement. If it is between 1 and 4, the if block is executed, which prompts the user to enter two numbers. After this the choice entered by the user is passed to the switch statement and it performs the relevant operation.

5.15 Write a program to enter a month number of year 2011 and display number of days present in the month.


Test Your Skills Now!
Take a Quiz now
Reviewer Name