In java, switch is one of the control statement which turns the normal flow control of the program as per conditions.In java, switch is one of the control statement which turns the normal flow control of the program as per conditions.
In java, switch is one of the control statement which turns the normal flow control of the program as per conditions. It's like if-else statement but it can reduce the number of if statements. When number of cases are limited then switch statement can be used. It checks your choice and jump on that case label if the case exists otherwise the statement with default label is executed. It works with the byte, short, char, int primitive data types and enumerated types.
Switch case is followed by a parenthesized integer
expression, that further follows cases, all enclosed in braces. The switch
statement executes the cases regarding the values of the expression. Code in a
case clause must ends with a
break statement, exiting with the
switch statement and continues with the statement following the switch. default
clause is executed if no corresponding case value exist. Execution continues
after the end of the switch statement if no case is matched and no default
If the switch case values does not matches with any
case value, control goes to the default clause. It is equivalent to the
"else" with the switch statement. default comes after the last
case that typically does not followed by
break because execution
just continues out the bottom of switch if this is the last clause.
breakstatement results in, passes the control to the statement after the end of the switch. Execution flows thru into the next case if no break exists. Passing control directly to the next case is almost always an error.
To know more about the switch statement click on: