The switch is a keyword defined in the java programming
language. Keywords are basically reserved words which have specific meaning
relevant to a compiler in java programming language likewise the switch
keyword indicates the following :
-- The switch statement in java language enables the user to select the
execution of one of code block from a group of similar code blocks on the
basis of an expression.
-- Always the switch condition must be evaluated to a char, byte, short or int. then only the execution of the switch case will start.
-- Within a switch case, a case block cannot be terminated itself i.e. it does not have any implicit ending point. So we provide a break statement at the end of each case block to exit from the switch statement.
-- If the break statement is lacking in the switch case then the flow of execution will flow into all following case and/or default
blocks. That makes senseless to use the switch case.Example to use the Switch keyword within a class in java
programming language:
switch with an int value:
|
int arg = <few values>; switch (arg ){ case 1: <statements> break; case 2: <statements> break; default: <statements> break; } |
switch with a char value:
|
char arg = <few values>; switch (arg) { case 'y': case 'Y': <statements> break; case 'n': case 'N': <statements> break; default: <statements> break; } |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: switch Java Keyword
Post your Comment