Switch Statement

72 views.

The Switch statement can be used for executing a group of statements based on a value.

Syntax

Program

Rule1: Specifying the argument to switch is mandatory and it should be of either byte, short, int or char only.

Rule2: Specifying the flower to switch statement is mandatory.

Rule3: Specifying the case and default statement in switch is optional.

Rule4: A switch statement can contain any number of cases and it can contain at most one default.

Rule5: The default statement can be specified anywhere in the switch statement.

Rule6: The case labels must be unique i.e. they should not be empty and they should not be duplicated.

Rule7: Specifying the break statement is optional. The break is a transfer statement and it will transfer the control from switch to outside the switch, so that it skips the execution of remaining cases.

Rule8: when a switch statement is executed, switch argument is compared with case labels and execution will begin from that case onwards whose label is matching with the argument and continues until encounters a break statement or until the end of switch.

Leave a Reply