Please tell me what is switch statement with its flow chart and with example.
Define Switch statement.
499
21-Jan-2020
Nishi Tiwari
21-Jan-2020Switch is a selection statement to execute a single statement from a list of multiple statement based on pattern match with the expression.
The switch expression is used with integer type such as int, char, byte, or short, an enumeration type, or of string type. The expression is checked for various cases and the one match is executed.
Switch can be used in place of if-else statement to provide better readability of code.
Syntax
Why do we use Switch Statements instead of if-else statements?
We generally use a switch statement instead of if-else statements because if-else statement works only for a small number of logical evaluations of a value. If we use if-else statement for a larger number of possible conditions then, it will take more time to write and also become difficult to read.