Switch Case Simplified in Java

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.

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 :statement;break;
case selectCase2 :statement;break;
case selectCase3 :statement;break;
default: 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.
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. Cases may contain zero or n lines
  7. There is no need to put braces for block of statements.
  8. Case labels ends with a colon(:).

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.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.