Switch case is uses as alternative of if….else. When we need to use long term of if…else then we use switch case for reducing program execution time.
syntax:
Switch(variable){
Case value 1:
Statement;
Break;
Case value 2:
Statement;
Break;
.
.
.
Default:
Statement;
Break;
}
In the switch variable we put the value which needs to match with case value. The value of variable are compare with all case value , if any of them are match with switch variable then program execute that case statement. If there are no matches found or false value passed in the switch variable then it execute default statement.
Switch(variable){
Case value 1:
Statement;
Break;
Case value 2:
Statement;
Break;
.
.
.
Default:
Statement;
Break;
}
In the switch variable we put the value which needs to match with case value. The value of variable are compare with all case value , if any of them are match with switch variable then program execute that case statement. If there are no matches found or false value passed in the switch variable then it execute default statement.
Here break keyword are use for end that particular process, that means break keyword stop the switch statement process. The break key word can execute only then, when any of the case can executed.