Switch Case Simplified in Java

Switch is a multi switch used often than if else. Switch in java is a conditional statement that activates only if a matching condition found out of the given input.We can use if else or if ladder instead of switch.But If else can be optimized using switch statements.Switch removes the nested if else and keep the code more user friendly,crisp and clean.When compared to high number of iterations the switch is faster as compared to if else statements.Switch is classified as selection statement.
Selector case is an expression the produces some value. the switch compares the result of selector case with selector case value.If a valid match is found the corresponding statement(s) executes.If no match occurs the default gets executed.In switch each case ends with a break which causes execution to jump to the end of select body but break statement is optional.If it is missing,the code for the following case statements executes until next break or end of select case.It is used to multi way selection  implementation.Even though characters can be used in switch,but actually down the line,it produces the integral value of a character.Byte or shorts can also be used in Switch. From Java -7 on words String values can be passed to Switch as well. However we can not enter real numbers in Switch.

the switch statement take the value of a variable or expression against a list of case values and when a match is found,a block of statements associated with case is executed.

Generic Syntax:

case selectCase1(constant1) :statement;break;
case selectCase2(constant2) :statement;break;
case selectCase3(constant3):statement;break;
default: statement;


SelectCase- It is the expression that evaluates an integral or String value.
Case -is the multiway switch position marked with case labels that accepts the case constants.
default-default is optional and provides default jump point when none of the value of the expression is listed in the case.I have written in detail in the below section
break-break statement is technically optional that make the JVM jump to the end of the switch statement.
An Example:

class MySwitchBoard{
public static void main(String args[])
int iSwitch=4;
case 0:
case 1:
case 2:
case 3:
case 4:

Another Example:

case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
case 'w':
System.out.println("some times vowel");
Importance of break:
break statement will break the switch statement once the condition is satisfied.So break will come out of the switch block. The control will not flow to the next case statement.It also signals the end of particular case and causes an exit from switch statement,referring the control out of the switch of the next statement.If we don not put the break statement (since it is optional) JVM will execute all the statements associated with next case label. It is legal but not desired.cases can be stacked on top of each other to provide multiple matches for a code.It is essential to put a break otherwise the control will go to the unwanted block of cases and execute.
Importance of default:
Default is an optional case,when present it will be executed if the value of the expression does not match with any of the case values.If not present,no action takes place when all matches fail and the control goes out of the switch statements.This will be executed if no condition matches with cases.So the remaining steps condition will be checked and finally execute the default statement.
There is no position for default.It can be placed anywhere in the switch block.Bast practice is to put to the end of the switch block.

Rules for switch:

  1. There is no limit for switch case
  2. switch takes input in the form of integer ,characters and String(java -8 onward).
  3. for non integral,non character or non string cases,we can not use switch rather a series of if statements are required.
  4. The select case values are constant expression known as labels
  5. The select cases are unique within a switch statement.
  6. We can club two or more cases together.
  7. Cases may contain zero or n lines
  8. There is no need to put braces for block of statements.
  9. Case labels ends with a colon(:).
Where can we use Switch in real time?
In menu items. A menu is a list of options.If the user selects one of the options.The JVM responds to each possible choice in a different way.
Read here for switch case in enum.

Switch Case Simplified in Java Switch Case Simplified in Java Reviewed by Animesh Chatterjee on October 22, 2018 Rating: 5

No comments:

Powered by Blogger.